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.
1 2 3 4 5 6 7 |
#import <uikit /UIKit.h>
@interface RootViewController : UITableViewController {
}
@property (nonatomic, retain) NSArray *arForTable;
@property (nonatomic, retain) NSMutableArray *arForIPs;
@end
|
4) Place following code in your “RootViewController.m” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#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.





hi vivian ,how to store the multi selected tableview rows in a single
NSString variable..thanks again
I will create a post based on ur demand ! Cheers :)