从UITableViewCell的accessoryView中删除视图无法正常工作

1 objective-c uitableview uiview accessoryview

我在我的单元格的accessoryView中设置了一个带有图像的uiview,后来我想要删除这个视图,以便可以再次显示accessoryType为none.以下不起作用 -

  //create cell
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

        //initialize double tick image
        UIImageView *dtick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dtick.png"]];
        [dtick setFrame:CGRectMake(0,0,20,20)];
        UIView * cellView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
        [cellView addSubview:dtick];

 //set accessory type/view of cell
        if (newCell.accessoryType == UITableViewCellAccessoryNone) {
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        else if(newCell.accessoryType == UITableViewCellAccessoryCheckmark){
                newCell.accessoryType = UITableViewCellAccessoryNone;
                newCell.accessoryView = cellView;
            }
        else if (newCell.accessoryView == cellView) {
            newCell.accessoryView = nil;
            newCell.accessoryType = UITableViewCellAccessoryNone;
          }
Run Code Online (Sandbox Code Playgroud)

我也试过[newCell.accessoryView reloadInputViews],但这也不起作用.

基本上我想循环通过这些状态点击单元格=>没有刻度 - >一个刻度 - >双刻度(图像) - >没有刻度

非常感谢任何帮助,谢谢.

Mar*_*n R 5

您的代码有两个问题:

  • newCell.accessoryView == cellView您将单元格的附件视图与新创建的图像视图进行比较时:此比较永远不会产生TRUE.

  • 将附件视图设置为图像时,还要将类型设置为UITableViewCellAccessoryNone,以便下次UITableViewCellAccessoryCheckmark再次设置.换句话说,else if永远不会执行第二个块.

以下代码可以工作(但我自己没有尝试):

if (newCell.accessoryView != nil) {
     // image --> none
     newCell.accessoryView = nil;
     newCell.accessoryType = UITableViewCellAccessoryNone;
} else if (newCell.accessoryType == UITableViewCellAccessoryNone) {
     // none --> checkmark
     newCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (newCell.accessoryType == UITableViewCellAccessoryCheckmark) {
     // checkmark --> image (the type is ignore as soon as a accessory view is set)
     newCell.accessoryView = cellView;
}
Run Code Online (Sandbox Code Playgroud)