ios 将 NSInteger 转换为 NSIndexpath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6454723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert NSInteger to NSIndexpath
提问by Dan
Basically I am storing an index of an array in a NSInteger. I now need it as an NSIndexpath, I'm struggling to see and find a way to convert my NSInteger to NSIndexpath so I can reuse it.
基本上我将数组的索引存储在 NSInteger 中。我现在需要它作为 NSIndexpath,我正在努力寻找并找到一种方法将我的 NSInteger 转换为 NSIndexpath 以便我可以重用它。
回答by ageektrapped
For an int index
:
对于 int index
:
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.
创建节点 0 中项目的索引以根据引用指向。
To use the indexPath in a UITableView
, the more appropriate method is
要在 a 中使用 indexPath UITableView
,更合适的方法是
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
回答by albertamg
If you need a NSIndexPath
for a UITableView
, you can use indexPathForRow:inSection:
(reference). Index paths passed to table view must contain exactly two indices specifying the section and row. An index path created with indexPathWithIndex:
only contains one index and won't work with a table view.
如果你需要NSIndexPath
的UITableView
,你可以使用indexPathForRow:inSection:
(参考)。传递给表视图的索引路径必须正好包含两个指定节和行的索引。创建的索引路径indexPathWithIndex:
仅包含一个索引并且不适用于表视图。
回答by Jhaliya
Use below methods of NSIndexPathclass.
使用以下NSIndexPath类的方法 。
+ (id)indexPathWithIndex:(NSUInteger)index;
+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length;
- (id)initWithIndex:(NSUInteger)index
Use as below and also Don't forget to release
myIndexPath
object after using.
如下使用,使用后也不要忘记release
myIndexPath
反对。
NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndex:[myIntObj intValue]];
回答by David Seek
Swift 3:
斯威夫特 3:
let indexPath = IndexPath(row: 0, section: 0)