view raw
Suchi Suchi - 10 months ago 51
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?


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;