有没有更方便的方法来创建NSIndexPath对象?

时间:2012-02-17 13:23:40

标签: iphone objective-c cocoa-touch

    NSIndexPath *index_path;
    NSUInteger   u_array[] = {0, 0, 0};                
    index_path = [NSIndexPath indexPathWithIndexes : u_array length : 3]; 

上面创建了一个长度为== 3的NSIndexPath。有没有办法用更少的语句来做同样的事情?

3 个答案:

答案 0 :(得分:3)

如果您正在谈论在UITableView中使用它,那就是UIKit addition indexPathForRow:inSection:

+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section

以下是如何使用它:

NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:3 inSection:4];

答案 1 :(得分:2)

这可能是你能做的最好的事情:

NSUInteger u_array[] = {0, 0, 0}; 
NSIndexPath *index_path = [NSIndexPath indexPathWithIndexes:u_array length:3];

答案 2 :(得分:1)

仅当您使用iOS时(您已标记此,因此不确定哪个)并且您只对两部分索引路径感兴趣(即表格部分和行) - 如果是这种情况,您可以使用[NSIndexPath indexPathForRow:inSection:]。这在文档的NSIndexPath UIKit添加部分中进行了描述。

相关问题