Question

I'd like to do following: After clicking a row in a tableview, I want to set value of some variable and in the same time change the cell accessory type to checkmark. Here is my implementation file:

- (void)viewDidLoad
{
    [super viewDidLoad];
    tabledata = [[NSArray alloc] initWithObjects:@"row1", @"row2", @"row3", nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableView Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tabledata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"Row"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Row"];
    }

    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark TableVoew Delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if row1 is selected change 'value' to 1 and change cell accessory to checkmark (other cells accessory to none)
//if row2 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
//if row3 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
}
Was it helpful?

Solution

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.value = indexPath.row;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

EDIT Set only the accessory for selected row in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

cell.accessoryType = UITableViewAccessoryNone;

if (indexPath.row == self.value) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

See this to remove checkmarks from currently visible cells in tableview.

EDIT 2 To unselect the previous row, do this in didSelectRowAtIndexPath

NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:index inSection:1];
NSLog(@"Oldindexpath is: %@", oldIndexPath);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

if (newCell.accessoryType == UITableViewCellAccessoryNone) {
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];

if (oldCell != newCell){
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    } 
}

OTHER TIPS

This solved my problem with removing old checkmarks:

for (UITableViewCell *cell in [tableView visibleCells]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

and problem with setting value of current row was solved in previous answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top