OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS Objective-c 的 UICollectionView 中创建无限滚动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:33
标题: ios - 如何在 iOS Objective-c 的 UICollectionView 中创建无限滚动

如何在我的 UICollectionView 中创建无限滚动?

附上截图:

enter image description here

现在我的 NSArray

NSArray* nameArr = [NSArray arrayWithObjects: @"0", @"1", @"2", @"3", @"4", nil];

我想在collectionView中创建一个Infinite Scrolling After Complete数组一次又一次地重复所有单元格。

无限滚动需要左右两边。

如果是,那我该如何实现,请在objective-c中给出引用。

谢谢!!!提前



Best Answer-推荐答案


首先将您当前的 NSArray 替换为 NSMutableArray 并在您的 cellForItemAtIndexPath 中进行上述更改

-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section {
    return nameArr.count;
}

- (__kindof UICollectionViewCell *) collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath {

     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier"Cell" forIndexPath:indexPath];

     UILabel *label = [cell viewWithTag:25];
     [label setText:[NSString stringWithFormat"%ld",(long)indexPath.row]];

     if(indexPath.row == nameArr.count-1) {
        [nameArr addObjectsFromArray:nameArr.mutableCopy];
        [collectionView reloadData];
     }
     return cell;
}

在这里,我们一次又一次地添加相同的数组对象,因此它将无限滚动。 附截图:Refer This

希望对你有帮助

关于ios - 如何在 iOS Objective-c 的 UICollectionView 中创建无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48076430/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4