[英]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.