Sagar R. Kothari Sagar R. Kothari - 1 year ago 118
Objective-C Question

UITableViewCell Accessory Type Checked on Tap & Set other unchecked

I am confused a little bit about settings table view cell accessories.

I have fixed two sections in my table

  • Home

  • Office

What I want is as follow....

  • When User tap any of the cell

  • Cell gets selected &

    • I want to set checked (set uitableviewcell accessory type -checked of tapped cell )

  • And also all other cell's accessory type now should set to

    • uitable view cell accessory type none

I have tried following code. But I found that indexpath.row & indexpath.section is readonly properties.

// Override to support row selection in the table view.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tblProfileView deselectRowAtIndexPath:indexPath animated:YES];
int i,max;
UITableViewCell *x; NSIndexPath *tt;

for(i=0;i<[tblProfileView numberOfRowsInSection:0];i++)
tt.row=i; tt.section=0;
x=[tblProfileView cellForRowAtIndexPath:];
[x setAccessoryType:UITableViewCellAccessoryNone];
for(i=0;i<[tblProfileView numberOfRowsInSection:1];i++)
tt.row=i; tt.section=1;
x=[tblProfileView cellForRowAtIndexPath:tt];
[x setAccessoryType:UITableViewCellAccessoryNone];

x=[tblProfileView cellForRowAtIndexPath:indexPath];
[x setAccessoryType:UITableViewCellAccessoryCheckmark];
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController animated:YES];
// [anotherViewController release];

Answer Source

I would keep track of the data that should be checked and change the cell in tableView:didSelectRowAtIndexPath: and update which data is checked in tableView:cellForRowAtIndexPath: like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // do usual stuff here including getting the cell

    // determine the data from the IndexPath.row

    if (data == self.checkedData)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the selected data from the IndexPath.row

    if (data != self.checkedData) {
       self.checkedData = data;

    [tableView reloadData];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download