NSIndexPath return 无
NSIndexPath return nil
我有一个 UIViewController
和 table 视图。 Table 行包含触发操作的按钮。
//This is my button selector inside tableview delegate cellForRowAtIndexPath
[myBtn addTarget:self action:@selector(onButtonPress:event:)forControlEvents:UIControlEventTouchUpInside];
//This is button action
-(void)onButtonPress:(UIButton*)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
controller.args=myArgs;
[self.navigationController pushViewController:controller animated:YES];
}
//this is my back button action
- (IBAction)onBackPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
一开始一切正常,但在我导航到其他 ViewController
和 return 后,按返回按钮返回到 table 查看控制器,然后再次单击 table 行按钮 NSIndexPath
为 nil
我试过这个indexPathForRowAtPoint returns nil only for first cell in a uitableview
如果我的理解是正确的,您在 TableViewCells 中有按钮,并且当您点击其中一个按钮时不会获得 IndexPath。
当然没有 IndexPath,因为您从未点击过单元格。基本上你忽略了完整的tableview机制。
创建 tableviewcell 时,用索引标记按钮。不是最好的方法,但可以。
将按钮的标签设置为 indexPath.row 并从标签中找到 NSIndexPath。
myBtn.tag=indexPath.row;
[myBtn addTarget:self action:@selector(onButtonPress:)forControlEvents:UIControlEventTouchUpInside];
-(void)onButtonPress:(UIButton*)sender {
UIButton *btnSender =(UIButton *)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btnSender.tag inSection:0];
}
我不会使用方法 indexPathForRowAtPoint:
来获取 indexPath
。您可以使用不同的方式将其存档。先添加一个UIView
分类:
头文件:
#import <UIKit/UIKit.h>
@interface UIView (TableCell)
-(UITableViewCell*)getTableCell;
@end
和实施
@implementation UIView (TableCell)
-(UITableViewCell *)getTableCell
{
while (self.superview) {
if ([self.superview isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell*)self.superview;
}else {
return [self.superview getTableCell];
}
}
return nil;
}
现在,在您的 onButtonPress:
中,您可以这样做:
UITableViewCell *cell = [sender getTableCell];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]
我建议你可以在'viewWillAppear'下写[tableView reload];
或使用其他方式如块来实现
尝试向下一个viewcontroller发送代表
DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
controller.args=myArgs;
controller.delegate = self;
[self.navigationController pushViewController:controller animated:YES];
并且当从 viewController 返回到现在时 table viewcontroller 通过该代表。
这可能对您有所帮助...
我有一个 UIViewController
和 table 视图。 Table 行包含触发操作的按钮。
//This is my button selector inside tableview delegate cellForRowAtIndexPath
[myBtn addTarget:self action:@selector(onButtonPress:event:)forControlEvents:UIControlEventTouchUpInside];
//This is button action
-(void)onButtonPress:(UIButton*)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
controller.args=myArgs;
[self.navigationController pushViewController:controller animated:YES];
}
//this is my back button action
- (IBAction)onBackPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
一开始一切正常,但在我导航到其他 ViewController
和 return 后,按返回按钮返回到 table 查看控制器,然后再次单击 table 行按钮 NSIndexPath
为 nil
我试过这个indexPathForRowAtPoint returns nil only for first cell in a uitableview
如果我的理解是正确的,您在 TableViewCells 中有按钮,并且当您点击其中一个按钮时不会获得 IndexPath。
当然没有 IndexPath,因为您从未点击过单元格。基本上你忽略了完整的tableview机制。
创建 tableviewcell 时,用索引标记按钮。不是最好的方法,但可以。
将按钮的标签设置为 indexPath.row 并从标签中找到 NSIndexPath。
myBtn.tag=indexPath.row;
[myBtn addTarget:self action:@selector(onButtonPress:)forControlEvents:UIControlEventTouchUpInside];
-(void)onButtonPress:(UIButton*)sender {
UIButton *btnSender =(UIButton *)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btnSender.tag inSection:0];
}
我不会使用方法 indexPathForRowAtPoint:
来获取 indexPath
。您可以使用不同的方式将其存档。先添加一个UIView
分类:
头文件:
#import <UIKit/UIKit.h>
@interface UIView (TableCell)
-(UITableViewCell*)getTableCell;
@end
和实施
@implementation UIView (TableCell)
-(UITableViewCell *)getTableCell
{
while (self.superview) {
if ([self.superview isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell*)self.superview;
}else {
return [self.superview getTableCell];
}
}
return nil;
}
现在,在您的 onButtonPress:
中,您可以这样做:
UITableViewCell *cell = [sender getTableCell];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]
我建议你可以在'viewWillAppear'下写[tableView reload]; 或使用其他方式如块来实现
尝试向下一个viewcontroller发送代表
DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
controller.args=myArgs;
controller.delegate = self;
[self.navigationController pushViewController:controller animated:YES];
并且当从 viewController 返回到现在时 table viewcontroller 通过该代表。
这可能对您有所帮助...