Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add checkboxes to UITableViewCell??

some time ago someone already asked this question and a few answers were given but i didn't really understand any of them. So i was wondering if anyone could please write an easy to understand tutorial on how to do the things shown on the image below:

http://img208.yfrog.com/img208/6119/screenshotkmr.png

I would be so greatful if anyone can share exactly how to this because it looks really cool and i would love to use something similar in my application :-)!

like image 315
Christoph v Avatar asked Sep 08 '10 10:09

Christoph v


2 Answers

Make a unchecked and checked image..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([selectedRowsArray containsObject:[contentArray objectAtIndex:indexPath.row]]) {
        cell.imageView.image = [UIImage imageNamed:@"checked.png"];
    }
    else {
       cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)];
    [cell.imageView addGestureRecognizer:tap];
    cell.imageView.userInteractionEnabled = YES; //added based on @John 's comment
    //[tap release];

    cell.textLabel.text = [contentArray objectAtIndex:indexPath.row];
    return cell;
}

- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
    CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];

    if ([selectedRowsArray containsObject:[contentArray objectAtIndex:tappedIndexPath.row]]) {
        [selectedRowsArray removeObject:[contentArray objectAtIndex:tappedIndexPath.row]];
    }
    else {
        [selectedRowsArray addObject:[contentArray objectAtIndex:tappedIndexPath.row]];
    }
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}
like image 143
LarsJK Avatar answered Oct 06 '22 00:10

LarsJK


One of the way I can think of is you do custom UITableViewCell (this tutorial is good for starting. This is another, similar one). Inside the custom UITableViewCell you need to put a UIButton there with a circle image. When user select the button, you change the image to the green circle

like image 41
vodkhang Avatar answered Oct 05 '22 23:10

vodkhang