ios - 将数据传递给自定义单元格中的 UIbutton

标签 ios uitableview custom-cell

我有一个带有自定义单元格的 TableView Controller 。 自定义单元格有一个按钮和一些文本标签。 单击时如何将行的文本数据传递给按钮?

最佳答案

1)在您的 cellForRowAtIndexPath: 中,将按钮标签指定为索引

cell.yourbutton.tag = indexPath.row;

2) 如下所示为您的按钮添加目标和操作。

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

3) ViewControler 中基于索引的 Action 代码如下。

-(void)yourButtonClicked:(UIButton*)sender{
 [arrayOfTextVaule objectAtIndex:sender.tag];}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
yourCustomCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",selectedCell.label);}

关于ios - 将数据传递给自定义单元格中的 UIbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28474830/

相关文章:

objective-c - iOS 自定义单元格可扩展 - 索引 0 问题

ios - Xamarin iOS MSBuild - 缺少 OutputPath?

ios - 核心数据 - 关系、获取和插入

ios - 将整个文本字段添加到 UITableView 以自动填充

ios - heightForRowAtIndexPath 设置错误行的高度

iOS 在哪里放置自定义单元格设计? awakeFromNib 或 cellForRowAtIndexPath?

ios - tableView 刷新时丢失自定义数据

ios - Tapjoy、Google Analytics 的设备宏 ID

iOS 下载前检查远程文件是否有更新

ios - 嵌套 UITableView 不返回任何单元格