【问题标题】:UITableViewCell which shows subview on touch and hides it on second touchUITableViewCell 在触摸时显示子视图并在第二次触摸时隐藏它
【发布时间】:2011-11-27 15:13:22
【问题描述】:

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

【问题讨论】:

  • 你的问题是?另外,到目前为止,您尝试了哪些方法,您查阅了哪些手册,为什么您的结果没有按照您的要求工作?
  • 我是 obj c 的新手,所以代码的结果很糟糕,我不明白这样做是正确的,添加带有控件的视图或添加简单的控件,以及如何制作动画子视图的出现和消失
  • 我不明白它是如何工作的,我使用 addSubview 将按钮添加到单元格,例如将单元格高度 50 和按钮 origin.y = 50 ,但是当我启动应用程序时,我可以看到这个按钮而不是隐藏在单元格内

标签: objective-c ios uitableview subview


【解决方案1】:

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,这样会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多