我想我不明白你在问什么。indexPath 是您刚刚创建的行的 IndexPath。您将属性设置为此值,但该属性将仅包含创建的最后一行的 IndexPath。
更新以显示示例:
方法 1 - 您没有从 cellForRowAtIndexPath 中调用其他方法。在这种情况下,您将需要一个 NSIndexPath 类型的私有属性(仅包含 MyViewController 导入和 @interface 声明的代码,以向您展示在何处放置私有属性声明:
在您的 .m 文件中:
#import "MyViewController.h"
@interface MyViewController ()
@property(nonatomic,strong) NSIndexPath *lastIndexPathCreated;
@end
@implementation MyViewController
@synthesize lastIndexPathCreated;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// All your other cell setup code
self.lastIndexPathCreated = indexPath;
return cell;
}
-(void)someOtherMethod {
// The last IndexPath of the row LAST created is self.lastIndexPathCreated
}
方法 2:假设您从 cellForRowAtIndexPath 中调用其他方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// All your other cell setup code
[self myOtherMethodWithIndexPath:indexPath];
return cell;
}
-(void)myOtherMethodWithIndexPath:(NSIndexPath *)lastIndexPath {
// Do something with lastIndexPath
}
希望这可以帮助