MultipleCheck in Table – UITableView

Hello All !

I am going to explain How to enable multiple checks using UITableView.

Here is the sample output of what I am going to explain using this post.

1) Create a new Project, Select Navigation Based Application & Click on next.

2) Set the project name as “multiCheck” as illustrated below.

3) Open “RootViewController.m” file. Place following code.

#import 
@interface RootViewController : UITableViewController {

}
@property (nonatomic, retain) NSArray *arForTable;
@property (nonatomic, retain) NSMutableArray *arForIPs;
@end

4) Place following code in your “RootViewController.m” file.

#import "RootViewController.h"
@implementation RootViewController
@synthesize arForTable = _arForTable;
@synthesize arForIPs = _arForIPs;
- (void)viewDidLoad {
	[super viewDidLoad];
	self.arForTable=[NSArray arrayWithObjects:@"Object-One",@"Object-Two",@"Object-Three",@"Object-Four",@"Object-Five", nil];
	self.arForIPs=[NSMutableArray array];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if([self.arForIPs containsObject:indexPath]){
	[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
	[cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.arForIPs containsObject:indexPath]){
	[self.arForIPs removeObject:indexPath];
    } else {
	[self.arForIPs addObject:indexPath];
    }
    [tableView reloadData];
}
@end

5) Run the project.

Download the source code from here.
Please let us know your comments on it.

2 thoughts on “MultipleCheck in Table – UITableView

Leave a message