【问题标题】:Removing view from UITableViewCell's accessoryView not working从 UITableViewCell 的附件视图中删除视图不起作用
【发布时间】:2012-07-27 04:42:04
【问题描述】:

我在单元格的附件视图中设置了带有图像的 uiview,稍后我想删除此视图,以便附件类型可以再次显示为无。以下不起作用 -

  //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] 但这也不起作用。

基本上我想在单击单元格时循环这些状态 => 无刻度 -> 一个刻度 -> 双刻度(图像)-> 无刻度

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

【问题讨论】:

    标签: objective-c uiview uitableview accessoryview


    【解决方案1】:

    你的代码有两个问题:

    • 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      相关资源
      最近更新 更多