我在单元格的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;
          }

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

基本上我想在单击单元格时循环显示这些状态=>无勾号->一勾号->双勾号(图像)->无勾号

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

有帮助吗?

解决方案

您的代码有两个问题:

  • 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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top