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

B.S*_*.S. 4 objective-c subview uitableview ios

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

Mun*_*ndi 5

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

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];
}
Run Code Online (Sandbox Code Playgroud)

然后只对您在相应的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
   }
}
Run Code Online (Sandbox Code Playgroud)

确保你的"特殊"单元格有所不同CellIdentifier,这将有效.