2

以 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 进行操作?

提前致谢。

4

3 回答 3

3

尝试使用创建索引路径indexPathForRow:inSection:

NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
于 2011-12-05T02:31:23.480 回答
2

我能想到的唯一一件事就是确保您拥有UIKit.framework作为您的框架之一。您可以通过按目标上“构建阶段”下的“将二进制文件与库链接”部分中的加号按钮来执行此操作。此外,请确保NSIndexPath.h您在尝试访问索引路径的文件中导入。

我以前遇到过这个问题,这解决了它。这是因为虽然NSIndexPathCocoa 和 CocoaTouch 之间是通用的,但 Cocoa 版本没有属性rowsection.

如果您已经拥有UIKit.framework,请尝试使用[NSIndexPath indexPathForRow:row inSection:section]而不是[NSIndexPath indexPathWithIndex:index].

于 2011-12-05T02:35:18.750 回答
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 进行操作”的意思吗?

于 2011-12-05T02:57:02.110 回答