我有一个带有自定义单元格的 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/