UITableViewCell在触摸时显示子视图并在第二次触摸时隐藏它

时间:2011-11-27 15:13:22

标签: objective-c ios uitableview subview

我需要创建特定的 tableViewCell ,它会在第一次触摸时显示一个特殊的子视图,并在第二次触摸时隐藏它该子视图包含一些标签或按钮。

1 个答案:

答案 0 :(得分:5)

cellForRowAtIndexPath中,将tag属性添加到相关单元格的子视图中。同时将子视图的hidden属性设置为YES。最后,将单元格的selectionStyle设置为UITableViewCellSelectionStyleNone

if (thisIsTheIndexPathInQuestion) {
   CGRect theFrame = CGRectMake(...); // figure out the geometry first
   UIView *subview = [[UIView alloc] initWithFrame:theFrame];
   // further customize your subview
   subview.tag = kSubViewTag; // define this elsewhere, any random integer will do
   subview.hidden = YES;
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   [cell.contentView addSubView:subview];
   [subview release];
}

然后只对您在相应的UITableView委托方法中描述的内容做出反应:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (thisIsTheIndexPathInQuestion) {  // you know how to check this
      UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
      UIView *subview = [cell viewWithTag:kSubViewTag];
      subview.hidden = !subview.hidden;  // toggle if visible
   }
}

确保您的“特殊”单元格具有不同的CellIdentifier,这样就可以了。