The TableViewCell reuse mechanism of ios avoids repeated display. iostableviewcell
The general configuration is as follows: when the range of tableView display is exceeded, the content displayed later will be repeated with the previous one.
// In this way, the content displayed beyond the page will appear again-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// define the unique identifier static NSString * CellIdentifier = @ "Cell"; // create a cell instance UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier] with a unique identifier; // Initialization is performed if it is null. (when the page is pulled, the previous cell is reused when the page is displayed more than the homepage content, instead of being initialized again.) if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];} // configure cell data in a simple manner. textLabel. text = @ "text"; cell. detailTextLabel. text = @ "text"; cell. imageView. image = [UIImage imageNamed: @ "4.png"]; return cell;} // solution 3
Solution 1: cancel the cell reuse mechanism. Using indexPath to create a cell can solve the problem of duplicate display. However, compared with big data, the memory is relatively tight.
// Solution 1 solve the problem of repeated display by preventing him from reusing the cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// define the unique identifier static NSString * CellIdentifier = @ "Cell"; // create a cell instance using indexPath. Each cell is a separate UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath]; // Initialization is performed if it is null. (when the page is pulled, the previous cell is reused when the page is displayed more than the homepage content, instead of being initialized again.) if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];} // configure cell data in a simple manner. textLabel. text = @ "text"; cell. detailTextLabel. text = @ "text"; cell. imageView. image = [UIImage imageNamed: @ "4.png"]; return cell ;}
Solution 2: Each cell has a corresponding identifier. This will make the cell reusable, so it will not repeatedly show that the display content is too large for memory usage. solution 1 is similar.
// Solution 2 also prevents reuse of cells. The difference is that each cell corresponds to a unique identifier-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// define the cell id. Each cell corresponds to its own identification NSString * CellIdentifier = [NSString stringWithFormat: @ "cell % ld", indexPath. section, indexPath. row]; // create the cell instance UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier] with different identifier; // initialize the cell instance if it is null. When the page is displayed more than the page content, the previous cell will be reused and will not be initialized again.) if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];} // configure cell data in a simple manner. textLabel. text = @ "text"; cell. detailTextLabel. text = @ "text"; cell. imageView. image = [UIImage imageNamed: @ "4.png"]; return cell ;}
Solution 3 as long as the last displayed cell content is not empty, delete all its sub-views. This is equivalent to separating the cell and then repeating the display with new data.
// Solution 3 when new data needs to be displayed on the page, you can customize the cell after deleting the last cell. This solution avoids repeated display, cell reuse is the best solution for memory management. The first two are relatively memory-consuming (UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// define the unique identifier static NSString * CellIdentifier = @ "Cell"; // create a cell instance UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier] with a unique identifier; // Initialization is performed if the result is null. (when the page is pulled, the previous cell is reused when the page content exceeds the homepage, Instead of re-initializing) if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];} else // when the page is pulled, when the cell exists and the last one exists, delete it and a unique cell. We can configure the data to avoid {while ([cell. contentView. subviews lastObject]! = Nil) {[(UIView *) [cell. contentView. subviews lastObject] removeFromSuperview] ;}// configure the cell with simple data. textLabel. text = @ "text"; cell. detailTextLabel. text = @ "text"; cell. imageView. image = [UIImage imageNamed: @ "4.png"]; return cell ;}All of the above are my personal understandings. I am also a cainiao. If you have any misunderstandings, I hope you can point them out and help me !! Thank you!