iPhone Development - How to create checkmarks in a UITableView

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.
[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (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:
[]
1
2
3
4
5
6
7
8
- (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!

Comments