UITableView 子视图到 uitableviewcell

Posted

技术标签:

【中文标题】UITableView 子视图到 uitableviewcell【英文标题】:UITableView subview to uitableviewcell 【发布时间】:2010-04-19 18:41:38 【问题描述】:

我正在尝试向 tableview 添加一个按钮,无论我指定什么框架,按钮都不可见。这是一个自定义 UITableViewCell,我在代码中而不是在 xib 文件中添加按钮,以便我可以单击按钮并执行不是 didSelectRowAtIndexPath 的操作: 我在 cellForRowAtIndexPath 中有以下代码:我错过了什么?

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = bframe;
    button.userInteractionEnabled = YES;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];
    return cell;

【问题讨论】:

如果不是太长,请在 cellForRowAtIndexPath 中显示完整的代码。单元格中添加了哪些其他控件可能会覆盖按钮?为什么不使用 cell.accessoryType = UITableViewCellAccessoryDe​​tailDisclosureButton 和 accessoryButtonTappedForRowWithIndexPath? 谢谢。我使用 UITableViewCellAccessoryDe​​tailDisclosureButton 和 accessoryButtonTappedForRowWithIndexPath 使其工作。 【参考方案1】:

试试这个

UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] 保留];

【讨论】:

感谢您的回复。这不起作用。 buttonWithType 为您进行分配和自动释放,所以我不认为这与我所做的有什么不同。

以上是关于UITableView 子视图到 uitableviewcell的主要内容,如果未能解决你的问题,请参考以下文章

UITableView - 分段的 Tableview 深入到子子视图然后进入详细视图

如何将 UITableView 作为子视图添加到 xib 中的 UIView?

编辑搜索栏时将 UITableView 添加到子视图

将子视图添加到 UINavigationBar 时向下推 UITableView

使用自动布局将子视图添加到 UITableView 不会设置其框架

UITableView 的子视图永远不会出现