Suchi Suchi - 1 year ago 95
iOS Question

✔ Checkmark selected row in UITableViewCell

I am an iOS development newbie. I want to add a checkmark to my

when it is selected. The checkmark should be removed when another row is selected. How would I do this?

Answer Source

In your UITableViewDatasource method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil )
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;

// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    self.lastIndexPath = indexPath;

    [tableView reloadData];

And lastIndexPath is a property(strong) NSIndexPath* lastIndexPath;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download