【问题标题】:How to properly toggle UITableViewCell's accesoryType on cell selection/deselection?如何在单元格选择/取消选择时正确切换 UITableViewCell 的 accesoryType?
【发布时间】:2012-01-15 14:46:20
【问题描述】:

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

 - (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! 我也试过打电话:

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

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

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

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

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

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

    【讨论】:

    • 快速和肮脏的:cell.accessoryType ^= UITableViewCellAccessoryCheckmark :)
    【解决方案2】:

    如果您只想将一行作为复选标记,请使用此

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

    【讨论】:

      【解决方案3】:

      再次点击单元格等同于选择单元格而不是取消选择。

      您需要在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法中进行切换以检查cell.accessoryType == UITableViewCellAccessoryCheckmark 是否存在。

      【讨论】:

        【解决方案4】:
        - (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
            [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
            UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
            if (cell.accessoryType == UITableViewCellAccessoryNone) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                // Reflect selection in data model
            } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
                cell.accessoryType = UITableViewCellAccessoryNone;
                // Reflect deselection in data model
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-08
          • 2014-07-17
          • 1970-01-01
          • 1970-01-01
          • 2021-08-24
          • 2011-04-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多