gpt4 book ai didi

objective-c - 使用 indexPathsForVisibleItems 时仅获取 NSIndexPath 的一部分

转载 作者:行者123 更新时间:2023-12-02 05:13:52 25 4
gpt4 key购买 nike

我是 Obj-C 的新手。我一直在使用 UICollectionView 的排序功能。我在细胞移动后保存数据。但是 NSIndexPath 信息并不像我预期的那样。我不确定在使用 indexPathsForVisibleItems 时如何只访问索引的一部分。

NSArray *visible = [self.collectionView indexPathsForVisibleItems];
NSLog(@"These are the visible cells %@", visible);

这会像这样返回 NSLogs....

"<NSIndexPath 0x8a6b7f0> 2 indexes [0, 0]",
"<NSIndexPath 0x8a6d3d0> 2 indexes [0, 2]",
"<NSIndexPath 0x8a6d480> 2 indexes [0, 3]",
"<NSIndexPath 0x889f080> 2 indexes [0, 1]"

我想获取第二列数字。 NSIndex 路径中的 0,2,3,1。我确定如果我可以访问这些值,我就可以从单元格中获取信息。是否有特定的约定或转换来获取 0,2,3,1 并转换为简单数组。

谢谢。

最佳答案

你可以做一些像...

NSArray *visible = [self.collectionView indexPathsForVisibleItems];
NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:[visible count]];
[visible enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
[rowsArray addObject:@(indexPath.item)];
}];

这将为您提供一个 NSNumber 对象数组,代表可见项目中 indexPaths 中的所有项目值。

如果您从 UICollectionView 使用它,您不应该真正使用 NSIndexPath.row 属性,因为这是旨在与 UITableView 一起使用。 NSIndexPath UIKit Additions供引用。

关于objective-c - 使用 indexPathsForVisibleItems 时仅获取 NSIndexPath 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14951508/

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