MultiChecking in tableView !

MultiChecking of tableView cells is one of the feature ie. is extremely important for every application and you’ll see this probably in every app.

Lets get Started !!!


Put up a TableView on your Nib file using interface builder. And add a button on it for printing the names of hobbies.


Open the (ViewController.h ) file and write the following code.

[csharp]<br /> #import <UIKit/UIKit.h></p> <p>@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate></p> <p>{</p> <p>NSArray *mutArr ;<br /> NSMutableArray *checkArr;<br /> NSMutableArray *checkArrData;<br /> }<br /> @property (nonatomic,retain)NSArray *mutArr;</p> <p>@property(nonatomic,retain)NSMutableArray *checkArr;</p> <p>@property (nonatomic,retain) IBOutlet UITableView *tableViewObj;</p> <p>- (IBAction)selectHobbies:(id)sender;</p> <p>@end</p> <p>[/csharp]

    1. Create an array “mutArr” which will contain the content to be displayed on the table.


    1. Another array for the storing the IndexPath of rows selected.


    1. last one will have the content of rows selected.


    1. We will also create an OUTLET for the tableView.


    1. Since i am storing the hobbies in “mutArr” , so i declared an action “selectHobbies“. When you will click on this button we will print the names of hobbies in the LOG.


STEP3 Now proceed with (ViewController.m).

Synthesise the Properties created in ( ViewController.h ).

[csharp]</p> <p>@synthesize mutArr,checkArr;</p> <p>@synthesize tableViewObj;</p> <p>[/csharp]

Now in ViewDidLoad method.

    1. Here we are just allocating the space to the arrays.


    1. And we here declared the


[csharp]</p> <p>self.mutArr=[[NSMutableArray alloc]initWithObjects:@"Cricket", @"Football",@"Gym",@"Clubbing",@"Drinking",@"Working Out ",@"Basketball",@"Iphone Programming", nil];</p> <p>self.checkArr=[NSMutableArray array];<br /> tableViewObj.dataSource=self;<br /> tableViewObj.delegate=self;</p> <p>[/csharp]


#pragma mark TableView Datasources

[csharp]<br /> – (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{</p> <p>return [self.mutArr count];</p> <p>}<br /> [/csharp]

[csharp]<br /> – (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{</p> <p>return @"Hobbies List";<br /> }<br /> [/csharp]

Am assuming there is no need to explain the above datasources , Since they are self explanatory.

[csharp]<br /> – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{</p> <p>static NSString *CellIdentifier = @"Cell";</p> <p>UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];</p> <p>if (cell == nil) {</p> <p>cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];</p> <p>}</p> <p>if([self.checkArr containsObject:indexPath]){</p> <p>[cell setAccessoryType:UITableViewCellAccessoryCheckmark];</p> <p>} else {</p> <p> [cell setAccessoryType:UITableViewCellAccessoryNone];</p> <p>}</p> <p>cell.textLabel.text=[self.mutArr objectAtIndex:indexPath.row];</p> <p>return cell;</p> <p>}<br /> [/csharp]

We will maintain a checkArr , containing all the

elements that are checked by the users.

When the first time the table is displayed the if statement “checkArr” will be empty and the else statement will get executed.

And we just setting the Text of the cell label in the second last line of code and then returning the cell.


#pragma mark TableView Delegates

[csharp]<br /> – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{</p> <p>[tableView deselectRowAtIndexPath:indexPath animated:YES];</p> <p>if([self.checkArr containsObject:indexPath]){</p> <p>[self.checkArr removeObject:indexPath];</p> <p>} else {</p> <p>[self.checkArr addObject:indexPath];</p> <p>}</p> <p>[tableView reloadData];</p> <p>}<br /> [/csharp]

The above delegate gets called every time the user clicks on the cell.

    1. Firstly we will deselect the row clicked.


    1. Then we will check whether the “checkArr” already contains the row selected. If selected it is removed from this array else it is add to the “checkArr”.


    1. Lastly the table is reloaded to reflect the changes.



[csharp]<br /> – (IBAction)selectHobbies:(id)sender{<br /> NSLog(@"%@",self.checkArr);<br /> }<br /> [/csharp]

The above will show the indexPath of all the rows selected.


Screen Shot 2013-04-15 at 11.52.15 PM

By | 2018-02-16T10:28:34+00:00 April 9th, 2013|How it works|0 Comments

About the Author:

Leave A Comment