Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

ios - What does registering a call for cell reuse actually do?

I don't totally understand what registering a class for cell reuse does. I understand how we use reuse identifiers on cells, I just don't understand what calling this method in viewDidLoad does. Looked at a bunch of docs. Not clicking, n00b here. Could someone give me some tips on what it does please?

TableViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register Class for Cell Reuse Identifier
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a UITableView. It has a datasource that provides it UITableViewCells. To save memory and processor cycles, it unloads UITableViewCells that are no longer on screen and puts them into a reuse queue. When it loads a new cell, the datasource will typically ask the UITableView for a cell from this reuse queue. If the queue is currently empty, UITableView will construct a new UITableViewCell using the class provided. The reuseIdentifier is used to distinguish this particular cell type queue from another cell type queue within the same UITableView.

Something like this:

UITableView: "Hey, Datasource! Give me the cell for this indexPath."

Datasource: "Alright. That's a 'foo' kind of cell. Got any of those kicking around that you're not using?"

No class registered; reuse cells returned from datasource previously

UITableView: "Yes, I do. Here you go."

No class registered; no cells available

UITableView: "Nope. Hey, I don't have a class registered for that kind of cell. Hmm. Here's nil instead."

Class registered; reuse cells returned from datasource previously

UITableView: "Yes, I do. Here you go."

Class registered; no cells available

UITableView: "Nope. But I have a class registered for that identifier. Here's a new instance."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...