iPhone Development – How to create checkmarks in a UITableView

iPhone UITableview Checkmarks

by joshgrenon on July 20, 2009

Welcome fellow iphone developers! If you are searching for a way to create checkmarks in a UITableView then you have come to the right place. I had to search high and low to figure out how to do this.

Let’s get to it then!

Problem: How does one create checkmarks in a UITableView for an iPhone application?
Solution: Set the accessory type of the UITableViewCell when it is selected or deselected.

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSUInteger row = [indexPath row];
 
   [tblSelections deselectRowAtIndexPath:indexPath animated:YES];
 
   if ([[tblSelections cellForRowAtIndexPath:indexPath ] accessoryType] == UITableViewCellAccessoryCheckmark)
   {
      [[tblSelections cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
      [selectedValues replaceObjectAtIndex:row withObject:@"0"];
   }
   else
   {
      [[tblSelections cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
      [selectedValues replaceObjectAtIndex:row withObject:@"1"];
   }
}

Note: You must also create an array in the viewDidLoad method to hold values for each option that is selected. Here is code to do that:

- (void)viewDidLoad
{
   NSMutableArray *arrayValues = [[NSMutableArray alloc] initWithObjects:@"0",@"0",@"0",nil];
   self.selectedValues = arrayValues;
   [arrayValues release];
 
   [super viewDidLoad];
}

I hope this is as helpful to you as it was to me!

Have a great day and remember we coders always have enough work to do!

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

blog comments powered by Disqus

Previous post:

Next post: