将NSInteger转换为NSIndexpath
基本上我在NSInteger中存储数组的索引。 我现在需要它作为一个NSIndexpath,我努力看到并find一种方法来将我的NSInteger转换为NSIndexpath,以便我可以重用它。
对于一个int index :
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
根据参考创build节点0中项目的索引 。
要在UITableView使用indexPath,更合适的方法是
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
如果你需要一个UITableView的NSIndexPath ,你可以使用indexPathForRow:inSection: ( reference )。 传递给表视图的索引path必须恰好包含两个指定节和行的索引。 使用indexPathWithIndex:创build的索引pathindexPathWithIndex:只包含一个索引,不能与表视图一起使用。
使用下面的NSIndexPath类的方法。
+ (id)indexPathWithIndex:(NSUInteger)index; + (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length; - (id)initWithIndex:(NSUInteger)index
如下所示使用,也不要忘记在使用后release myIndexPath对象。
NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndex:[myIntObj intValue]];
Swift 3:
let indexPath = IndexPath(row: 0, section: 0)