我有一个表格 View ,里面有一个单元格。这个单元格有一个按钮,在 customTableVIewCell 类中声明如下。
@property (weak, nonatomic) IBOutlet UIButton *myButton;
在 TableView Controller 中,我正在为此按钮设置方法。
[cell.myButton setTitle:@"Show cell number!" forState:UIControlStateNormal];
[cell.myButton addTarget:self action:@selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
我想在选择按钮时更改按钮的标题。
现在考虑一下: 我的表格 View 中有三个单元格,也就是说我的表格有三行。 在每一行中,都有一个名为 myButton 的按钮。
每当单击它时,我想根据按钮所在的单元格编号将按钮的标题更改为 1 或 2 或 3。
我试过这个:
- (IBAction)myButtonClicked:(id)sender{
NSIndexPath *indexPath =[self.tableVIew indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
NSLog(@"button was clicked for the following row %i", row);
}
但是,无论单元格 #1 或单元格 #2 或单元格 #3 上的按钮是否被单击,这都会向控制台打印“按钮被单击了下一行 0”。 (单元格只是表格行)
请帮帮我。
最佳答案
尝试将按钮标签设置为-
cell.myButton.tag = indexPath.row;
在你的 cellForRowAtIndexPath 方法中..
然后在您的 myButtonClicked 方法中获取发件人按钮的标签......然后做任何您想做的事。
- (IBAction)myButtonClicked:(id)sender{
NSLog(@"button was clicked for the following row %i", sender.tag);
}
关于ios - 在我的表格 View 的自定义单元格中更改按钮的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25907646/