是否可以在表视图中将多个对象或数据传递给addtarget?

时间:2017-05-08 10:49:18

标签: ios objective-c uitableview

我想在btn上传递textview值,点击特定的单元格索引。 我的代码是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

-----
  [cell1.submitCommentBtn addTarget:self action:@selector(leftCommentBtnClicked:) forControlEvents:UIControlEventTouchUpInside];   // here i want to pass textview object or its data  
---

}

1 个答案:

答案 0 :(得分:0)

试试这个

使用视图层次结构

   - (IBAction) leftCommentBtnClicked:(UIButton *)sender {
        UITableViewCell *cell =(UITableViewCell *)sender.superview.superview.superview; //change with your class
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; // using this indexpath you can get data from your datasource array
        cell.textView; // this is your textview 
    }

OR

使用ConvertPoint

- (IBAction) leftCommentBtnClicked:(UIButton *)sender {
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
         NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
         YourCell *cell = (YourCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        cell.textView; // this is your textview 
    }