zoukankan      html  css  js  c++  java
  • UITableView优化之按需加载

    滑动UITableView时,按需加载对应的内容

    直接上代码:

    //按需加载 - 如果目标行与当前行相差超过指定行数,只在目标滚动范围的前后指定3行加载。
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
        NSIndexPath *ip = [self indexPathForRowAtPoint:CGPointMake(0, targetContentOffset->y)];
        NSIndexPath *cip = [[self indexPathsForVisibleRows] firstObject];
        NSInteger skipCount = 8;
        if (labs(cip.row-ip.row)>skipCount) {
            NSArray *temp = [self indexPathsForRowsInRect:CGRectMake(0, targetContentOffset->y, self.width, self.height)];
            NSMutableArray *arr = [NSMutableArray arrayWithArray:temp];
            if (velocity.y<0) {
                NSIndexPath *indexPath = [temp lastObject];
                if (indexPath.row+33) {
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-3 inSection:0]];
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-2 inSection:0]];
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]];
                }
            }
            [needLoadArr addObjectsFromArray:arr];
        }
    }

    记得在tableView:cellForRowAtIndexPath:方法中加入判断:

    if (needLoadArr.count>0&&[needLoadArr indexOfObject:indexPath]==NSNotFound) {
        [cell clear];
        return;
    }
  • 相关阅读:
    《大话设计模式》读书笔记
    设计模式个人笔记
    多线程的单元测试工具
    设计模式六大原则
    时间复杂度和空间复杂度(转)
    排序算法笔记
    《人月神话》读书笔记
    微信公众号开发踩坑记录(二)
    微信公众号开发踩坑记录
    全栈工程师之路
  • 原文地址:https://www.cnblogs.com/lilufeng/p/5217889.html
Copyright © 2011-2022 走看看