gpt4 book ai didi

ios - Objective-C错误:使用NSArray的单元上的索引路径错误

转载 作者:行者123 更新时间:2023-12-01 19:34:09 26 4
gpt4 key购买 nike

我通过Swift Bridging在我的代码中为我的CollectionView使用CardsCollectionViewLayout

Objective-C(无效)

我的问题是IndexPath返回错误的Item索引。这是最小的代码:

- (void)viewDidLoad
{
[super viewDidLoad];

self.collectionViewEvents.collectionViewLayout = [[CardsCollectionViewLayout alloc] init];
self.collectionViewEvents.dataSource = self;
self.collectionViewEvents.delegate = self;
self.collectionViewEvents.pagingEnabled = YES;
self.collectionViewEvents.showsHorizontalScrollIndicator = NO;

[self load];
}

// Issue visible here, the indexPath.item is not correct
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
for (UICollectionViewCell *cell in self.collectionViewEvents.visibleCells) {
NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
return;
}
}

- (void)load
{

// self.eventData is declares as: @property (nonatomic) NSArray *eventData;
self.eventData = [[NSArray alloc] initWithObjects:UIColor.blackColor, UIColor.whiteColor, UIColor.brownColor, nil];
[[self collectionViewEvents] reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCellReuseIdentifier"
forIndexPath:indexPath];

cell.layer.cornerRadius = 7.0;
cell.backgroundColor = UIColor.blackColor;

return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.eventData.count;
}

结果(注意第一个和第二个单元格如何具有相同的IndexPath项):
2020-03-19 14:48:05.334905+0100 App[7422:2617858] Visible Cell IndexPath Item 2 # => 3rd cell
2020-03-19 14:48:05.741805+0100 App[7422:2617858] Visible Cell IndexPath Item 1 # => 2nd cell
2020-03-19 14:48:06.184932+0100 App[7422:2617858] Visible Cell IndexPath Item 1 # => 1st cell

迅捷(工作中)

我尝试从他的代码存储库尝试 this Example,该存储库已静态声明 colors:
  var colors: [UIColor]  = [
UIColor(red: 237, green: 37, blue: 78),
UIColor(red: 249, green: 220, blue: 92),
UIColor(red: 194, green: 234, blue: 189),
UIColor(red: 1, green: 25, blue: 54),
UIColor(red: 255, green: 184, blue: 209)
]
...

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCellReuseIdentifier", for: indexPath)

cell.layer.cornerRadius = 7.0
cell.backgroundColor = .black

return cell
}


在执行以下代码后,它返回正确的IndexPath。
    // Issue NOT visible here, the indexPath.item IS correct
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

for cell in collectionView.visibleCells {
let indexPath = collectionView.indexPath(for: cell)
print(indexPath?.item)
return
}
}

结果:
2020-03-19 14:48:05.334905+0100 App[7422:2617858] Visible Cell IndexPath Item 2 # => 3rd cell
2020-03-19 14:48:05.741805+0100 App[7422:2617858] Visible Cell IndexPath Item 1 # => 2nd cell
2020-03-19 14:48:06.184932+0100 App[7422:2617858] Visible Cell IndexPath Item 0 # => 1st cell

我的 Objective-C代码在做什么错?

我尝试过的事情
// 1st cell has index 1, instead of 0
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
for (UICollectionViewCell *cell in [[self collectionViewEvents] visibleCells]) {
NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
return;
}
// 1st cell has index 1, instead of 0
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSArray *visible = [self.collectionViewEvents indexPathsForVisibleItems];
NSIndexPath *indexPath = [visible firstObject];
NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
return;
// Calling it in main thread, same result
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_main_queue(), ^{
for (UICollectionViewCell *cell in [[self collectionViewEvents] visibleCells]) {
NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
return;
}
});

最佳答案

问题是您在此循环中有返回值。

for (UICollectionViewCell *cell in [[self collectionViewEvents] visibleCells]) {
NSIndexPath *indexPath = [[self collectionViewEvents] indexPathForCell:cell];
NSLog(@"Visible Cell IndexPath Item %ld", indexPath.item);
return; // <- This here
}
visibleCells返回集合中所有可见单元的数组。您只可以NSLog该数组中的第一项,因此它取决于您滚动到第一项的位置。

关于ios - Objective-C错误:使用NSArray的单元上的索引路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60758739/

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