简体   繁体   English

为什么在制作自定义表格视图单元格时两次使可重用单元格出队

[英]Why dequeue reusable cell twice in making custom table view cell

I am following a tutorial of making custom table view cell with storyboard. 我正在学习使用情节提要制作自定义表格视图单元的教程。 I drag a UILabel as subview of the cell and set its tag to 1. I have two questions regarding the data source code. 我将UILabel拖动为单元格的子视图,并将其标签设置为1。关于数据源代码,我有两个问题。

  1. What's the purpose of the second dequeue statement? 第二个出队声明的目的是什么? I know it's an init method while not using storyboard to make the custom cell. 我知道这是不使用情节提要制作自定义单元格的一种初始化方法。

  2. What's the difference between tableview and self.tableview? tableview和self.tableview有什么区别?

     -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } NSDictionary *dToAccess = (self.tableView==tableView)?[self.arForTable objectAtIndex:indexPath.row] : [self.arForSearch objectAtIndex:indexPath.row]; [(UILabel*)[cell viewWithTag:1] setText:[dToAccess valueForKey:@"name"]]; [(UILabel*)[cell viewWithTag:2] setText:[dToAccess valueForKey:@"value"]]; return cell; } 

For your first question, the second dequeueReusableCellWithIdentifier: looks like a mistake. 对于第一个问题,第二个dequeueReusableCellWithIdentifier:看起来像一个错误。

Here is how a UITableView works: 这是UITableView的工作方式:

You might have 50 rows in your table, but if only 10 rows are visible at a time, you only need to make 10 cells, and then when the user scrolls, you can reuse cells that have gone offscreen instead of always releasing them and init'ing new cells that come onscreen. 您的表中可能有50行,但是如果一次仅可见10行,则只需要创建10个单元格,然后在用户滚动时,您可以重复使用屏幕外的单元格,而不必总是释放它们并进行初始化出现在屏幕上的新细胞。 A UITableView keeps a list of cells that have gone offscreen and when you call dequeueReusableCellWithIdentifier:, it removes it from the list of offscreen cells and returns it to you. UITableView保留了屏幕外的单元格列表,当您调用dequeueReusableCellWithIdentifier:时,它将其从屏幕外单元格列表中删除并将其返回给您。 From here you can customize the cell for re-use (change its text, color, etc) and return it. 在这里,您可以自定义单元格以供重复使用(更改其文本,颜色等)并返回它。 Again, this is not an "init" method, this is returning a pre-existing cell. 同样,这不是“ init”方法,它返回一个预先存在的单元格。

So, let's look at what happens when this UITableView is first displayed -- in this example there are 10 visible cells, so the tableView will call tableView:cellForRowAtIndexPath: 10 times to get cells to display in these 10 slots. 因此,让我们看一下第一次显示此UITableView时会发生什么-在此示例中,有10个可见单元格,因此tableView将调用tableView:cellForRowAtIndexPath:10次,以使这10个插槽中显示单元格。 Every time this is called, you will need to initialize and return a new UITableViewCell to display. 每次调用此方法时,都需要初始化并返回一个新的UITableViewCell进行显示。 (At this point dequeueReusableCellWithIdentifier: will return nil, because you don't have any offscreen cells to re-use yet) (这时dequeueReusableCellWithIdentifier:将返回nil,因为您还没有任何可重用的屏幕外单元格)

When a user scrolls your list, cells will begin to go offscreen, and new cells will need to appear. 当用户滚动您的列表时,单元格将开始移出屏幕,并且需要显示新的单元格。 You don't need to make new cells, because you have already created as many as will need to be onscreen at a time. 您无需制作新的单元格,因为您已经创建了需要一次在屏幕上显示的单元格。 You should call dequeueReusableCellWithIdentifier: to get a reference to a cell that has gone offscreen, which you can then re-use. 您应该调用dequeueReusableCellWithIdentifier:以获取对已离开屏幕的单元的引用,然后可以重新使用该单元。

I would alter your code like this: 我会像这样更改您的代码:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

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

Now you are checking for reusable cells before creating new ones. 现在,您要在创建新单元之前检查可重用单元。

For your second question, 关于第二个问题,

In your example, tableView refers to the tableView that was passed in (see the "tableView" in your method signature). 在您的示例中,tableView引用传入的tableView(请参见方法签名中的“ tableView”)。 Separately, if your class has defined a property called tableView, then self.tableView will call the getter for this property. 另外,如果您的类定义了一个名为tableView的属性,则self.tableView将为此属性调用getter。

When apple developed the UITableView for the first iphone they had a problem in performance when scrolling through it. 当苹果公司为第一台iPhone开发UITableView时,在滚动浏览时它们的性能存在问题。 Then one clever engineer discovered that the cause of this was that allocation of objects comes with a price, so he came up with a way to reuse cells. 然后,一位聪明的工程师发现了这种情况的原因是对象的分配带来了代价,因此他想出了一种重用单元的方法。

dequeueReusableCellWithIdentifier method is used to returns a cell if it has been marked as ready for reuse. 如果已将单元格标记为可以重用,则dequeueReusableCellWithIdentifier方法用于返回单元格。

So Whenever there are many number of rows in a table view and you are going to scroll it, then the cells which are just passed away from your previous screen before scrolling are get reused instead of creating new one. 因此,只要表格视图中有很多行并且要滚动它,那么在滚动之前刚从上一个屏幕传递出去的单元格就会被重用,而不是创建新的单元格。

And to know the ans of your second que. 并知道您第二个问题的答案。 I think you should refer this link : 我认为您应该参考此链接:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/17669-when-use-self-objectname-just-objectname.html http://www.iphonedevsdk.com/forum/iphone-sdk-development/17669-when-use-self-objectname-just-objectname.html

不必两次出队,此代码块将被破坏。

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

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