iphone - 如何将复选框添加到 UITableViewCell?

标签 iphone objective-c uitableview sdk

前段时间有人问过这个问题并给出了一些答案,但我并不真正理解其中的任何一个。所以我想知道是否有人可以写一个易于理解的教程来说明如何执行下图所示的操作:

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

如果有人能确切地分享如何做到这一点,我将非常感激,因为它看起来真的很酷,我很乐意在我的应用程序中使用类似的东西:-)!

最佳答案

制作未勾选和勾选图像..

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

关于iphone - 如何将复选框添加到 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666629/

相关文章:

ios - iPhone NSXMLParser 解析字符串并存储在 NSNumber 变量和不同的数据类型中

.net - 有没有一种简单的方法可以为整个应用程序设置默认字体?

ios - 如何在 iOS 中的 soap 请求后刷新 tableview

ios - 如何使 View Controller 转到随机 View Controller

objective-c - 执行获取请求 :error: causes an uncaught exception 'NSRangeException'

ios - KVO 不工作(观察另一个类的属性)

iPhone iOS 是否有用于类似代数计算器的应用程序的表达式解析器?

iphone - 如何防止由 NSFetchedResultsChangeInsert 和 insertRowsAtIndexPaths 插入的屏幕外单元格调用 cellForRowAtIndexPath

ios - 滚动表上的 ProgressView block

ios - 从抽屉 Controller 退出左 View Controller