简体   繁体   中英

Is it necessary to implement the dequeue mechanism for reusable UITableViewCells when displaying only a very few cells?

I have about five cells in my table and it is certain that there aren't going to be more. Do I still need to implement the dequeue mechanism of reusing the cells or is it OK to create the cells straight away?

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

It sounds like, in your case, you need not worry about caching and re-using cells. But it is a "best practice" to do so. If you code your app to re-use cells, and later your app changes and you have more than just a few cells, you'll get the benefit of cell re-use. There isn't too much overhead added by this anyway, so you might as well. Or not. It's up to you! Point is: If you don't want to, then don't. And in this case, you'll see little impact from not re-using cells, IMO.

You are not forced to reuse cells. It is just to improve your app's performance. It won't do any harm for your amount of cells not to reuse them, but why won't you do it? It is not much more code (just a simple call) and you are scalable.

You can certainly do so, especially when your tableView does not scroll. I would consider it good practice to use the dequeue mechanism anyway, you might want do add cells in the future and mght not think about not having it implemented in the first place, and after all, it is not much code to write, you could even do it in two lines (would be a mess to read though):

static NSString *Identifier = @"MyIdentifier";
UITableViewCell *cell = ((cell = [UITableView dequeueReusableCellWithIdentifier:Identifier]))?cell:[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:Identifier];

The double brackets are for silencing the compiler warning for using the result of an assignment in an if-statement. (I havent tested this, it should work though.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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