UITableViewCell复用机制

来源:互联网 发布:mac 弹丸论破 言刃 编辑:程序博客网 时间:2024/05/20 11:32

  • 默认UITableViewCell加载
  • UITableViewCell复用
  • 加载机制实现
  • 注意事项

默认UITableViewCell加载

当使用UITableView时,IOS默认当加载方式是:启动->加载一屏的cell,滚动时接着进行加载其他cell,这样做的坏处是,当应用程序有很多cell的时候不停的加载造成内存吃紧,应用程序变的卡顿。

UITableViewCell复用

UITableViewCell复用机制的原理很简单,就是创建一屏的cell,滚动时将消失的cell放在接下来要出现的cell。

加载机制实现

-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //第一步,先去内存中查找标志为cell的UITableViewCell                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];    //第二步,如果不存在就创建一个标志为cell的UITableView    if (cell == nil) {        cell  = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];    }     //第三步,设置cell中要显示的值       cell.textLabel.text = [NSString stringWithFormat:@"第%ld组第%ld行",indexPath.section,indexPath.row];    return cell;}

注意事项

在设置cell中显示内容时(第三步),不要new UIView, 这样同样会造成内存吃紧,因为这个方法是每次都要调用的。

0 0
原创粉丝点击