iOS 优化UITableView和UICollectionview

本文介绍了在iOS开发中,如何使用UICollectionView的runloop和scrollView代理来优化图片加载过程,确保在滑动结束后开始加载图片,提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用runloop的小操作

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    PlaysListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemcell" forIndexPath:indexPath];

     /**

      runloop - 滚动时候 - trackingMode,

      - 默认情况 - defaultRunLoopMode

      ==> 滚动的时候,进入`trackingMode`,defaultMode下的任务会暂停

      停止滚动的时候 - 进入`defaultMode` - 继续执行`trackingMode`下的任务

      */

     [self performSelector:@selector(p_loadImgeWithIndexPath:)

                       withObject:indexPath

                       afterDelay:0.0

                          inModes:@[NSDefaultRunLoopMode]];

    return cell;

}

2、使用ScrollView的代理 判断当前是否在滑动中

//核心判断:tableView非滚动状态下,才进行图片下载并渲染

- (void)p_loadImage{

 

    //拿到界面内-所有的cell的indexpath

    NSArray *visableCellIndexPaths = self.collectionView.indexPathsForVisibleItems;

 

    for (NSIndexPath *indexPath in visableCellIndexPaths) {

 

        PlaysHomeListModel *model = self.datasource[indexPath.row];

 

        PlaysListCollectionViewCell *cell = (PlaysListCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

        [cell configViewWithModel:model];

    }

}

//手一直在拖拽控件

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

 

    [self p_loadImage];

}

 

//手放开了-使用惯性-产生的动画效果

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

 

    if(!decelerate){

        //直接停止-无动画

        [self p_loadImage];

    }else{

        //有惯性的-会走`scrollViewDidEndDecelerating`方法,这里不用设置

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值