简体   繁体   English

iPhone表格视图-表格单元格内的按钮

[英]iphone table view - button inside table cell

I have created a button and placed it inside a table cell: 我创建了一个按钮并将其放置在表格单元格内:

[btn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
...
[cell.aSubView addSubview:btn];

The button shows up in the table cell, but tapping it highlights and selects the entire cell. 该按钮显示在表格单元格中,但是点击它会突出显示并选择整个单元格。 How can I just get the button to be selected on its own? 我如何才能自己选择按钮?

Why do you use cell.aSubView ? 为什么使用cell.aSubView You should go with the contentView property. 您应该使用contentView属性。 From the documentation : 文档中

If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode. 如果要通过简单地添加其他视图来自定义单元格,则应将其添加到内容视图中,以便在单元格进入和退出编辑模式时它们的位置适当。

Set your cell's selectionStyle property to UITableViewCellSelectionStyleNone . 将单元格的selectionStyle属性设置为UITableViewCellSelectionStyleNone Make sure tableView:didSelectRowAtIndexPath: handles the tap on the cell properly (eg ignores it if that is what you want). 确保tableView:didSelectRowAtIndexPath:正确处理单元格上的点击(例如,如果需要的话,将其忽略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM