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

发布于 2024-12-18 09:14:41 字数 102 浏览 1 评论 0原文

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

I need to create specific tableViewCell ,which shows a special subview on first touch and hides it on second touch That subview contains some labels or buttons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被翻牌 2024-12-25 09:14:41

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 并且这将起作用。

In cellForRowAtIndexPath, add a tag property to the subview of the cell in question. Also set the subview's hidden property to YES. Finally, set the cell's selectionStyle to 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];
}

Then just react to what you describe in the appropriate UITableView delegate method:

- (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
   }
}

Make sure your "special" cell has a different CellIdentifier and this will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文