Ibz
Ibz

Reputation: 518

UITableView Cell with CheckMarks

I have got an iphone project that has a UITableView which is populated from Core Data..

When a cell is selected, the user is directed to a new view controller (see code line below) which shows additional information about that cell entry (from core data)

[self presentModalViewController:noteViewController animated:YES]

What i want to do is to be able to set some sort of checkmark on either side of each cell to represent whether the task is complete of incomplete..

I have read that i could do this using:

UITableViewCellAccessoryCheckmark

but i am not sure how to implement is as currently

didSelectRowAtIndexPath:

is being used to present the modal view controller..

So essentially i want to know if there is a method for changing the state of a cell's accessory from checkmark to nothing (and vice versa) independently.. in the sense that if the actual checkmark is touch.. it shows it.. and if it is touched again, it hides it..

Any help would be greatly appreciated!

Upvotes: 1

Views: 10997

Answers (2)

visakh7
visakh7

Reputation: 26400

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

  if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
  {
    selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
  else 
  if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
  {
   selectedCell.accessoryType = UITableViewCellAccessoryNone;
  }
   //Do something
  }

Hope this helps

UPDATE

This tutorial will give you a basic info on custom table view cell. Another Reference.

I think this will give you the solution.

Upvotes: 6

MarkPowell
MarkPowell

Reputation: 16540

You will need to track selected rows externally from Cell presentation. This means, your model (that you used to build the cells in the first place) will need to track some sort of boolean. Once you have that you set accessoryType to the proper type.

i.e.

if ([[data objectAtIndex:indexPath.row] isSelected]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

This would be some of the logic in your:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 9

Related Questions