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.

Select 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.

Select Code
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.

Did you like this? Share it:

About Sagar R. Kothari

Hey ! Hi there ! I am Sagar R. Kothari. I am graduated from Gujarat University. I am pursuing M.C.A from IGNOU University. I am in iOS App/Game Development since last 3 years. I am always hunting for better knowledge in iOS. I am looking forward to be a good app & game developer. I keep on sharing my knowledge @ http://sugartin.info as soon as I get spare time for it. I would love to hear your personal feedback about this blog.

2 comments on “MultipleCheck in Table – UITableView

Leave a Reply

Your email address will not be published. Required fields are marked *


+ eight = 11

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">