zoukankan      html  css  js  c++  java
  • UITableView的重用

    在tableView上显示数据时, 我们需要创建cell来显示每一行的具体数据,当一个tableView数据量较大的时候,连续地alloc出cell会占用大量的内存,这时候就引出了tableView的重用队列。
    原理:
    只需创建屏幕所能显示的cell数+1个cell, 当某个cell完全出屏幕时, 即加入重用队列;
     当某一行即将显示到屏幕时, 需要一个cell来显示内容,这时它不会马上创建而是会先去重用队列找是否有可用的cell,如果有相同标记的cell,则可以直接拿来用,否则则需要重新创建 。
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //  1.声明一个cell的标记
        static NSString *cell_id = @"cell_id";
        //  2.创建UITableViewCell,并置空
        UITableViewCell *cell = nil;
        //  3.到重用队列中查找, 是否有可用的cell
        cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
        //  4. 如果没有找到可重用的cell,就只能自己创建了
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id] autorelease];
        }
        //  5.可以正常使用
        cell.imageView.image = [UIImage imageNamed:@"angel"];
        
        cell.textLabel.text = (_dic[_keyArray[indexPath.section]])[indexPath.row];
        
        //  6.返回cell
        return cell;
    }  
  • 相关阅读:
    交换机技术
    第七周课后总结
    以太网原理
    test
    NetCore第一步:千里之行 始于环境构筑
    第二十课(一)
    第十九课(三)
    第十九课(二)
    第十九课(一)
    第十八课(三)
  • 原文地址:https://www.cnblogs.com/liuyu521/p/3754911.html
Copyright © 2011-2022 走看看