Question

I have add one SegementController in CellForRowAtIndex method and its selected lasted value which we select previously. But once we move to other screen and come back to current then Segement will select the multiple option like this :

enter image description here

Here "Yes" is already selected and then i select "No",

Here is the code which i write in CellForRowAtIndexPath :

   UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Yes", @"No", @"Either", nil]];
   seg.frame = CGRectMake(190.0f, 10.0f, 205.0f, 30.0f);
   seg.selectedSegmentIndex = 0;
   [seg setTintColor:[UIColor orangeColor]];
   seg.segmentedControlStyle = UISegmentedControlStyleBar;
   [seg addTarget:self action:@selector(segSelected:) forControlEvents:UIControlEventValueChanged];
   seg.tag = indexPath.row;
   cell.textLabel.frame = CGRectMake(10, 5, 150, 30);
   [cell.contentView addSubview:seg];
   seg.selectedSegmentIndex = [[[arryaSegment objectAtIndex:indexPath.row]objectForKey:@"SelecteKey"] intValue];
Was it helpful?

Solution

This is probably not about selecting 2 segments at the same time, but adding two segmentedControls at the same time because you add a new control each time the cell is displayed.

Make sure the code that adds the UISegmentedControl is only called once per cell.

e.g:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell; // = ... dequeue ...
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

        // add segmented control only when creating a new cell
        UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Yes", @"No", @"Either", nil]];
        seg.frame = CGRectMake(190.0f, 10.0f, 205.0f, 30.0f);
        seg.selectedSegmentIndex = 0;
        [seg setTintColor:[UIColor orangeColor]];
        seg.segmentedControlStyle = UISegmentedControlStyleBar;
        [seg addTarget:self action:@selector(segSelected:) forControlEvents:UIControlEventValueChanged];
        cell.textLabel.frame = CGRectMake(10, 5, 150, 30);
        [cell.contentView addSubview:seg];

        seg.tag = 42; // used to reference segmentedControl in dequeued cell. see next line
    }

    UISegmentedControl *seg = (UISegmentedControl *)[cell.contentView viewWithTag:42];
    seg.selectedSegmentIndex = [[[arryaSegment objectAtIndex:indexPath.row]objectForKey:@"SelecteKey"] intValue];

    return cell;
}

since you can't use the ugly way of getting the indexPath with the tag of the segmentedControl anymore, you have to use a method which gets the indexPath in a different way. Something like this:

- (IBAction)segSelected:(UISegmentedControl *)sender {
    CGPoint originOfSegmentedControlInTableView = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:originOfSegmentedControlInTableView];
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top