如何在单元格选择/取消选择时正确切换UITableViewCell的accesoryType? [英] How to properly toggle UITableViewCell's accesoryType on cell selection/deselection?

查看:110
本文介绍了如何在单元格选择/取消选择时正确切换UITableViewCell的accesoryType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在选择/取消选择表格单元格时切换 accesoryType ...行为应该是:点按 - >将accessoryType设置为 UITableViewCellAccessoryCheckmark - >点按再次单元格 - >回滚到 UITableViewCellAccessoryNone 类型。
我的控制器中的实现如下:

I'm trying to toggle accesoryType when a table cell is selected/deselected... the behavior should be: tap -> set accessoryType to UITableViewCellAccessoryCheckmark -> tap the cell again -> rollback to UITableViewCellAccessoryNone type. The implementation in my controller is the following:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

...无论如何将样式配置为 UITableViewCellAccessoryCheckmark 我无法将其恢复为 UITableViewCellAccessoryNone
我也试着打电话:

...anyway once the style is configured as UITableViewCellAccessoryCheckmark I'm unable to restore it back to UITableViewCellAccessoryNone! I also tried to call:

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

但不删除复选标记...我该怎么办?

but does not remove the checkmark... what should I do?

编辑:实现没问题,问题出在自定义UITableViewCell子类中......对不起:P

推荐答案

如果这是你想要的话,试试这个

Try this if this is what you want

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {   
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

这篇关于如何在单元格选择/取消选择时正确切换UITableViewCell的accesoryType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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