0

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)
}
TomasJ
  • 289
  • 8
  • 21

2 Answers2

0
- (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;
    } 
}
Community
  • 1
  • 1
Pulkit Goyal
  • 5,604
  • 1
  • 32
  • 50
  • so this 'indexPath.row' is integer value from 0 to 2 in my case right? – TomasJ Oct 24 '12 at 09:34
  • Yes, `indexPath.row` is between `0` and `2`. – Pulkit Goyal Oct 24 '12 at 09:43
  • And one more question: I need just one row to be selected so when another one is selected, previous should not has checkmark on it. And when program is started I want the first row to be selected. – TomasJ Oct 24 '12 at 09:47
  • So this code you added made my first row checkmarked and after selecting others I couldn't uncheck them. Was that what it should done? – TomasJ Oct 24 '12 at 10:10
  • Just go through all visible cells and set the accessory to none. – Pulkit Goyal Oct 24 '12 at 10:19
  • it's ok but still can't figure out how to remove chceckmarks but it's another topic. Thanks – TomasJ Oct 24 '12 at 12:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18519/discussion-between-tomasj-and-pulkit-goyal) – TomasJ Oct 24 '12 at 13:34
  • Still don't unselect previous row. And there is a semantic issue in the first line of your last code which says: Incompatibile pointer to integer conversion sending 'char *(const char *, int)' to parameter of type 'NSInteger' (aka 'int'); – TomasJ Oct 24 '12 at 14:11
0

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.

TomasJ
  • 289
  • 8
  • 21