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;
    }  
  • 相关阅读:
    win7下安装IIS
    C#在处理多线程更新到UI控件的多种方法
    更新DataGridVeiw中的数据到后台数据库中
    ArcGIS Engine App update
    C#中提供的精准测试程序运行时间的类Stopwatch
    ArcMap10 生成随机点
    HDU 2111 Saving HDU
    HDU 1213 How Many Tables
    HDU 2521 反素数
    HDU 1995 汉诺塔V
  • 原文地址:https://www.cnblogs.com/liuyu521/p/3754911.html
Copyright © 2011-2022 走看看