[英]Unable to access tableview property to get the index of button clicked in custom cell
i have created a custom table cell app . 我创建了一个自定义表格单元格应用程序。 each cell contains buttons.
每个单元格包含按钮。 i found on stackoverflow that the best way to find out whether button in row 1 was clicked or row 2 was clicked was to use this method
我在stackoverflow上发现,找出是否单击了第1行中的按钮或单击了第2行中的按钮的最佳方法是使用此方法
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
however i cannot use this because i am not getting an error at self.tableView. 但是我不能使用它,因为我在self.tableView上没有收到错误。
this is because instead of using a UItableviewcontroller i am using UIViewcontroller and implementing the tableviewdelegate and tableviewdatasource. 这是因为我不是使用UItableviewcontroller,而是使用UIViewcontroller并实现了tableviewdelegate和tableviewdatasource。 and the viewcontroller does not have any property tableview.
并且viewcontroller没有任何属性tableview。
i cannot change the UIViewcontroller to UItableViewcontroller because everywhere in the code i am pushing the viewcontrollers using fileowner 我无法将UIViewcontroller更改为UItableViewcontroller,因为在代码中的任何地方,我都在使用fileowner推送viewcontrollers
So how can i get rid of the self.tableview error 所以我该如何摆脱self.tableview错误
My suggestion would be to keep the indexPath.row value in the custom cell object right when you create it. 我的建议是在创建索引路径时将indexPath.row值保留在自定义单元格对象中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your cell creation logic here.
//Set indexpath in the custom cell.
cell.parentIndexPath = indexPath;
return cell;
}
and when the button is clicked, you can check it inside the custom cell object: 并单击该按钮时,可以在自定义单元格对象中对其进行检查:
-(IBAction) buttonClicked: (id) sender {
NSLog(@"User clicked on index path %@", self.parentIndexPath);
}
You have to declare the parentIndexPath in the custom cell header (.h) file. 您必须在自定义单元头(.h)文件中声明parentIndexPath。
@property (nonatomic, strong) NSIndexPath *parentIndexPath;
try to create property for your table 尝试为您的表创建属性
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
then set myTableView instead of self.tableview. 然后设置myTableView而不是self.tableview。 i tried this and this one works for me well
我尝试了这个,这个很适合我
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:myTableView];
NSIndexPath *indexPath = [buddyListTableView indexPathForRowAtPoint:buttonPosition];
NSLog(@"Index Path :%d",indexPath.row);
This should work: 这应该工作:
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:(UITableView *)self.view];
But you should have an @property
which points to the table view really. 但是您应该有一个
@property
,它实际上指向表视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.