In a UITableViewCell of the second section of my UITableView, i have defined a UISegmentedControl composed of two buttons:
if (indexPath.section == 1 && indexPath.row == 0)
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
allerRetour = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects:NSLocalizedString(@"Y aller", nil), NSLocalizedString(@"En partir", nil), nil]];
[allerRetour setFrame:CGRectMake(9.0f, 0.0f, 302.0f, 45.0f)];
allerRetour.selectedSegmentIndex = 0;
[cell addSubview:allerRetour];
}
return cell;
Now, when i click on a button of that two buttons in the segmented control, i want to track that, so i try this in the didSelectRowAtIndexPath but it doesn’t work (The NSLog is not displaying, so the event isn’t thrown):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section==1 && indexPath.row==0)
{
if (allerRetour.selectedSegmentIndex == 0)
{
labelModeRecherche.text=@"Départ";
NSLog(@"Départ");
}
else
{
labelModeRecherche.text=@"Arrivée";
NSLog(@"Arrivée");
}
}
}
Clicking on the actual segmented control will not cause a didSelectRowAtIndexPath. Instead, you need to add a target, like so:
And define the “action:” method to respond to the event.
Then put the code of the inside if in that action method.