ios - 在我的表格 View 的自定义单元格中更改按钮的标题

标签 ios objective-c uitableview

我有一个表格 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/

相关文章:

ios - Xcode7 : How to mail crash report in swift

iphone - 在 iphone 中创建类似 facebook 菜单导航的 View

iphone - 如何在单击按钮时获取 UILabel 中的 UIDatePicker 值?

ios - 从特定行获取 Picker 数据

ios - UITableView 滑动操作删除和编辑自定义 View

ios - 从 UIWebView 捕获信息

ios - Google 和 Facebook 在同一个应用程序中登录

ios - 单击搜索栏上的取消按钮时如何弹出自身 View Controller ?

ios - 在 PFQuery 上调用 .whereKey 方法

ios - 自 iOS 13.4 起不再显示购买对话框