Ios 是否可以在表视图中将多个对象或数据传递给addtarget?
我想在btn上传递textview值,单击特定单元格索引。 我的代码如下:Ios 是否可以在表视图中将多个对象或数据传递给addtarget?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想在btn上传递textview值,单击特定单元格索引。 我的代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ----- [cell1.submitCommentBtn addTarget:self action:@selector(leftCommentBtnClicked:) forControlEvents:UI
- (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
---
}
试试这个
使用视图层次结构
- (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
}
或
使用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
}
这里是IndexPath,因此可以获得该特定行的所有值。可以为多个对象传递数组或nsdictionary是否有在选择器中传递数据的选项?我不想为此使用任何额外的内容。textview是否包含单元格?[UITableViewCellContentView text]:发送到实例0x7FF3EA4AFD10的无法识别的选择器现在检查我的更新答案,因为您需要在该行中跟踪superView,以便
- (void)leftCommentBtnClicked:(UIButton*)sender
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:(Your Table Name)];
NSIndexPath *indexPath = [(Your Table Name) indexPathForRowAtPoint:buttonPosition];
}