如何向 UITableViewCell 添加复选框? [英] How to add checkboxes to UITableViewCell??

查看:31
本文介绍了如何向 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 :-)!

推荐答案

制作未选中和选中的图像..

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];
}

这篇关于如何向 UITableViewCell 添加复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆