How To Add Default Check Box On TableView Cell in iOS App

Today in this tutorial, I will show you, how to put UITableView in editing mode and bring beautiful default check box on table view cell. This is a very useful feature which you will need to implement in most of the iOS app.

Now go to Storyboard in your project and find UITableView object. Drag drop a UITableView object  on your ViewController scene.  Then Drag drop a UIButton object on your ViewController scene.

Now create an reference outlet for your UITableView object in your ViewController.h file and name it as mTableView. Create an IBAction for your UIButton object and name it as didTapBringCheckBoxBtn

Now select your ViewController.h file  and Append “<UITableViewDelegate, UITableViewDataSource>” after “UIViewController”. Declare an NSArray and name it as dataArray.

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *dataArray;
}

Now your ViewController.h file should look something like the following image.

 

select your ViewController.m file and under -(void)viewDidLoad method, write the following code to populate our dataArray and set the delegate and datasource of our UITableView object.





- (void)viewDidLoad {
    [super viewDidLoad];
    
    
 dataArray=[[NSMutableArray alloc]init];
    [dataArray addObject:@"Infosys"];
    [dataArray addObject:@"IBM"];
    [dataArray addObject:@"Wipro"];
    [dataArray addObject:@"TCS"];
    [dataArray addObject:@"Google"];
    [dataArray addObject:@"PAytm"];
    [dataArray addObject:@"Apple"];
    [dataArray addObject:@"Cotiviti"];
    [dataArray addObject:@"Excellence"];
    [dataArray addObject:@"Capgemini"];
    [dataArray addObject:@"Microsoft"];
self.tblData.delegate=(id)self; self.tblData.dataSource=(id)self; }

Now its time to implement our UITableView DataSource methods to populate data in tableView.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray 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];
    }
    
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}


Now run the project and see the data listing of your tableView. But you will not see any check box on table view.



Now Its time implement the check box part of our project. Implement the following UITableView delegate method in your project. When user will select an item, didSelectRowAtIndexPath method will get fired, and when user will de- select an item, didDeselectRowAtIndexPath method will get fired.



- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"user de-selected %@",[dataArray objectAtIndex:indexPath.row]);
}

Now finally add the following code under your 


- (IBAction)didTapBringCheckBoxBtn:(id)sender {
    
     [_tblData setEditing:YES animated:YES];
}



Congrats!!! You have successfully built a Table View app with default check box. Now run the project and enjoy your table view multi cell selection feature. You can download the entire project from here. This is a basic tutorial for tableView edit mode and I will encourage you to do a lots of experiment with this code . If you have found this tutorial as helpful, then please do share it with your friends using the social sharing button available below. You can also subscribe to this blog to get the email notification of other iOS tutorial . Bye for now. Happy coding.

Comments

Post a Comment

Most Visited

Software Test Engineer Sample Resume

Progress Indicators (Loading...)