gpt4 book ai didi

objective-c - 如何从 NSIndexPath 获取特定值

转载 作者:太空狗 更新时间:2023-10-30 03:36:46 26 4
gpt4 key购买 nike

我有一个 NSArray,里面有 NSIndexPaths

NSArray *array = [self.tableView indexPathsForSelectedRows];

for (int i = 0; i < [array count]; i++) {
NSLog(@"%@",[array objectAtIndex:i]);
}

NSLog 返回这个:

<NSIndexPath 0x5772fc0> 2 indexes [0, 0]
<NSIndexPath 0x577cfa0> 2 indexes [0, 1]
<NSIndexPath 0x577dfa0> 2 indexes [0, 2]

我试图从 indexPath 中获取第二个值到一个普通的 NSInteger

最佳答案

您可以使用NSIndexPath-indexAtPosition: 方法获取最后一个索引:

NSIndexPath *path = ...; // Initialize the path.
NSUInteger lastIndex = [path indexAtPosition:[path length] - 1]; // Gets you the '2' in [0, 2]

在您的情况下,您可以使用以下内容(正如 Josh 在他的评论中提到的,我假设您正在使用 UITableView 的自定义子类,因为它没有特定的方法 (-indexPathsForSelectedRows:):

NSArray *indexes = [self.tableView indexPathsForSelectedRows];
for (NSIndexPath *path in indexes) {
NSUInteger index = [path indexAtPosition:[path length] - 1];
NSLog(@"%lu", index);
}

这将打印出 0, 1, 2, ...

关于objective-c - 如何从 NSIndexPath 获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com