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;
    }
  • 相关阅读:
    程序员写 2000 行 if else?领导:这个锅我不背
    var_dump
    CURL常用命令
    Socket阻塞模式和非阻塞模式的区别
    php框架之odp(一)
    git命令之git clone用法
    git push origin master和git push有什么区别?
    YouTube上最火的十个大数据视频
    Java两种核心机制
    Java四类八种数据类型
  • 原文地址:https://www.cnblogs.com/lilufeng/p/5217889.html
Copyright © 2011-2022 走看看