简体   繁体   English

我如何知道点击UItableview中的Cell按钮

[英]how do i know what Cell's button in UItableview clicked

I have a UITableView with custom Cell (subclass of UITableViewCell). 我有一个UITableView与自定义Cell(UITableViewCell的子类)。 Each cell view has two buttons btn1 and btn2. 每个单元格视图都有两个按钮btn1和btn2。 I set my ViewController to handle the TouchUp Inside of those btn1s. 我设置我的ViewController来处理那些btn1s的TouchUp Inside。

How do I know in which cell button clicked? 如何知道点击了哪个单元格按钮?

I'd put a tag property on it. 我在上面放了一个标签属性。 Say the left one has a tag of 1, and the right has a tag of 2. All UIViews have a tag property, which is just an integer value. 假设左边的标记为1,右边的标记为2.所有UIView都有标记属性,它只是一个整数值。

@property (nonatomic) NSInteger tag;

Then when you have received a button tap, you can ask what the button's tag is - 1 or 2, and you'll know which button it was. 然后,当您收到按钮时,您可以询问按钮的标签是什么 - 1或2,您将知道它是哪个按钮。

If you want to know which index path that button belonged to, you can grab the button's superview, which should be the cell, or the button's superview's superview, if you placed it on the cell's content view (as you generally should): 如果你想知道那个按钮所属的索引路径,你可以抓住按钮的superview,它应该是单元格,或者按钮的superview的superview,如果你把它放在单元格的内容视图上(通常你应该这样):

UITableViewCell *cell = (UITableViewCell *)[button superview];
NSIndexPath *pathForSelectedButton = [tableView indexPathForCell:cell];

Note that the above code assumes the button was placed directly on the cell. 请注意,上面的代码假定按钮直接放在单元格上。

UIButton has a tag property. UIButton有一个标签属性。 You can set a unique value to each of your button, such as indexPath.row*2 and indexPath.row*2 + 1 if there is only one section. 您可以为每个按钮设置唯一值,例如indexPath.row * 2和indexPath.row * 2 + 1(如果只有一个部分)。

And then you can add target method for each button. 然后您可以为每个按钮添加目标方法。

[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]

Lastly, implement buttonPressed method like 最后,实现buttonPressed方法

- (void)buttonPressed:(UIButton *)sender {
    NSInteger tag = sender.tag;
    // your code here.
}

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

相关问题 我如何知道是否选择了UITableView的单元格? - How can I know if a cell of UITableView is selected? 我如何知道用户是否单击了按钮? - How do I know if the user has clicked on a button? 如何向 uitableview 的每个单元格添加一个按钮? - How do I add a button to each cell of the uitableview? 如何从分组的UITableView的单元格中删除边框? - How do I remove the border from a grouped UITableView's cell? UITableView:如何在单击按钮时动态更改单元格高度? - UITableView: How to change cell height dynamically when a button is clicked in it? UITableView:如何在单击按钮时动态更改单元格高度? 迅速 - UITableView: How to change cell height dynamically when a button is clicked in it? Swift 当按下TableView单元格中的按钮时,我怎么知道按下了哪一行单元格的按钮? - When the button in TableView cell pressed, how do I know the button of which row of cell pressed? 当我不知道会有多少节时,如何重新加载UITableView的数据? - How do I reload a UITableView's data when I don't know how many sections there will be? 如何在原型单元格上使用UITableView单元格及其元素 - How do I use a UITableView cell and its elements on prototype cell 单击单元格中的按钮时,如何解析播放某首歌曲? - How do I get parse to play a certain song when a button in the cell is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM