UITableViewCell-了解“可重用" [英] UITableViewCell - Understanding "reuseable"

查看:38
本文介绍了UITableViewCell-了解“可重用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <代码>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态NSString * reuseIdentifier = @"cellReuse";UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];if(cell == nil){单元格= [[UITableViewCell分配] initWithStyle:UITableViewCellStyleDefault重用标识符:reuseIdentifier];}NSManagedObject * manageObject = [self.fetchResultController objectAtIndexPath:indexPath];cell.textLabel.text = [manageObject valueForKey:@"title"];cell.detailTextLabel.text = [manageObject valueForKey:@"subtitle"];返回单元} 

我从一本书中读到,单元格是一种可重用的视图".我在理解可重用"方面遇到一些问题.

有人能简单地解释细胞机制吗?

解决方案

问题:表视图可能具有数千行(或数百万行,无论如何).为每个数据行创建一个单独的单元将是乏味且浪费的.取而代之的是,表格视图只要求它同时显示在屏幕上的行数(通常不超过10-15-20个单元格).这是可管理的,不会占用大量内存,并且由于并非所有行都可以在显示器上看到,因此可以很好地发挥作用.

因此,当表视图需要显示一个新的单元格时(因为用户已滚动视图),它会取出一个超出可见区域的单元格,并将其排回到最后,重新使用./p>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"cellReuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        }

        NSManagedObject *manageObject = [self.fetchResultController objectAtIndexPath:indexPath];
        cell.textLabel.text = [manageObject valueForKey:@"title"];
        cell.detailTextLabel.text = [manageObject valueForKey:@"subtitle"];

        return cell;
}

I've read from a book that cell is a kind of "reuseable view". I have some problems in understanding the "reusable".

Could anybody explain in an easy way about the mechanism of cell?

解决方案

The problem: a table view can potentially have thousands of rows (or millions, whatever). It would be tedious and wasteful to create a separate cell for each data row. Instead, the table view only asks for as many rows as it displays on the screen at the same time (this is typically no more than 10-15-20 cells). This is manageable, doesn't consume a lot of memory and plays nice with the fact that not all of the rows are visible on the display anyway.

So, when the table view needs a new cell to be displayed (because the user has scrolled the view), it takes a cell that went out of the visible area, and queues it back to the end, reusing it.

这篇关于UITableViewCell-了解“可重用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆