简体   繁体   English

UITableView中的标识符的目的是什么

[英]What is the purpose of an Identifier in UITableView

I'm a little confused to why an identifer (*MyIdentifier) is always required. 我对为什么总是需要标识符(* MyIdentifier)感到有些困惑。 The code below demonstrates this. 下面的代码演示了这一点。 I have noticed all tableviews require at least one. 我注意到所有表视图都至少需要一个。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}

What is the purpose behind having an identifier? 拥有标识符的目的是什么? Ive have seen a few tutorials where there is more than one. 我见过一些教程,其中有不止一个。 Also, reading the Apple documentation, I was a little confused about why the following is called: 另外,在阅读Apple文档时,我对为什么调用以下内容感到有些困惑:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}

I would like to know why it takes the input of 'MyIdentifier'? 我想知道为什么要输入“ MyIdentifier”? Assuming we had more than one identifier, which one should we choose? 假设我们有一个以上的标识符,我们应该选择哪个? To be exact, what if we had a Parent, Child and sub-child etc. 确切地说,如果我们有父母,子女和子子女等,该怎么办?

From the docs: 从文档:

The identifier is a string identifying the cell object to be reused. 标识符是一个字符串,用于标识要重用的单元格对象。 This parameter must not be nil. 此参数不能为nil。

For performance reasons , a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. 出于性能原因 ,当表视图的数据源将单元格分配给其tableView:cellForRowAtIndexPath:方法中的行时,通常应重用UITableViewCell对象。 A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. 表视图维护数据源已标记为可重复使用的UITableViewCell对象的队列或列表。 Call this method from your data source object when asked to provide a new cell for the table view. 当要求为表格视图提供新的单元格时,请从数据源对象中调用此方法。 This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. 如果一个可用的单元格可用,则此方法使该单元出队,或者使用您先前注册的类或nib文件创建一个新的单元格。 If no cell is available for reuse and you did not register a class or nib file, this method returns nil. 如果没有可供重用的单元格,并且您没有注册类或nib文件,则此方法返回nil。

Every time you return a cell you have 2 options - you can create new cell and return it, dequeue cell that already exists and reconfigure it. 每次返回单元格时,都有2个选项-您可以创建新单元格并返回它,使已经存在的单元格出队并重新配置它。

So the way this works is when you create cell you give it reuse identifier, so when cell goes off screen it can be used instead of creating new cell. 因此,此方法的工作方式是在创建单元格时为其提供重用标识符,因此当单元格离开屏幕时,可以使用它代替创建新的单元格。 After the cell is dequeued though, you might want to change its properties(like text or image) 不过,单元格出队后,您可能想要更改其属性(例如文本或图像)

You can have different reuse identifiers for different cell types(different content views) 您可以为不同的单元格类型使用不同的重用标识符(不同的内容视图)

Suppose you have several customized UITableViewCells and each of them have different backgound colors. 假设您有几个自定义的UITableViewCells,并且每个都有不同的背景色。 In your storyboard or xib file, you may name the cell with red background color "redCell" and the one with blue "blueCell". 在情节提要或xib文件中,您可以将单元格命名为具有红色背景色“ redCell”,并将其命名为具有蓝色“ blueCell”。 Then you can select what kind of cell to add to a particular row using their identifiers. 然后,您可以使用其标识符选择要添加到特定行的单元格类型。 Let's say, you wanna apply red cells to odd rows and blue ones to even rows then you can use the following code to do this: 假设您要对奇数行应用红色单元格,对偶数行应用蓝色单元格,然后可以使用以下代码执行此操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   UITableViewCell *redCell = [tableView dequeueReusableCellWithIdentifier:@"redCell"];

   UITableViewCell *blueCell = [tableView dequeueReusableCellWithIdentifier:@"blueCell"];

   if (indexPath.row % 2 == 0) return redCell;
   else return blueCell;
}

Without specifying an identifier, the system wouldn't know which kind of cell to pick. 如果不指定标识符,系统将不知道选择哪种单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM