nsindexpath访问行到section属性会导致SIGABRT?

时间:2011-12-05 02:15:22

标签: ios nsindexpath

定位iOS 5.0当我尝试在以下代码中访问NSIndexPath对象的行或节属性时,我看到了SIGABRT:

NSIndexPath* path = [[NSIndexPath alloc] initWithIndex:0];
int row = path.row; //SIGABRT
int section = path.section;//SIGABRT

此示例建议使用indexPathWithIndex:而不是http://developer.apple.com/library/mac/#samplecode/SourceView/Listings/BaseNode_m.html#//apple_ref/doc/uid/DTS10004441-BaseNode_m-DontLinkElementID_6

path = [NSIndexPath indexPathWithIndex:0];

结果保持一致。有没有人解释为什么会发生这种情况?是不是我们在iOS 5.0中无法对NSIndexPath进行操作?

先谢谢了。

3 个答案:

答案 0 :(得分:3)

尝试使用indexPathForRow:inSection:

创建索引路径
NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];

答案 1 :(得分:2)

我唯一能想到的就是确保你有UIKit.framework作为你的框架之一。您可以通过按下" Link Binary With Libraries"中的加号按钮来完成此操作。 "构建阶段"在你的目标上。另外,请确保在您尝试访问索引路径的文件中导入NSIndexPath.h

我之前遇到过这个问题,这就解决了。这是因为尽管{4}在Cocoa和CocoaTouch之间是通用的,但Cocoa版本没有属性NSIndexPathrow

如果您已经section,请尝试使用UIKit.framework代替[NSIndexPath indexPathForRow:row inSection:section]

答案 2 :(得分:0)

我认为问题是indexPathWithIndex:创建一个单节点的索引路径。对于initWithIndex:相同: section和row属性从UITableView类别到NSIndexPath类。单节点索引路径对于与表视图一起使用是无效的,因为传递给表视图的索引路径必须包含指定节和行的两个索引(根据我运行代码时获得的错误消息)。以下代码创建一个带有两个索引的索引路径,并在不抛出NSInternalInconsistencyException的情况下运行:

NSUInteger x[] = {0 , 0};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
int row = [path row];
int section = [path section];
row = path.row;
section = path.section;

这是“你在iOS 5.0中对NSIndexPath进行操作”的意思吗?