Tableviewcontroller cell Reuse Error in iOS (content disorder)-Pinterest
Time
Use my hands to make your dreams---brother
Today, my beloved program appeared bug, for me this side dish, have to say is very distressed, mainly is the cell loading time because of the problem of reuse pool appeared various bugs, although the program did not collapse, but greatly affect my mood, the following is the most important problem, Is the cell reuse problem, cell because from the reuse pool, did not delete the contents of the content in a timely manner resulting in the various occurrences, there are several solutions.
UITableView inherits from Uiscrollview, a scroll-based control that Apple encapsulates for us. The above is mainly a uitableviewcell, you can let UITableViewCell respond to a few click events, you can also add Uitextfield or Uitextview in the UITableViewCell, and other sub-views, Makes it possible to edit text on the cell.
The cell in the UITableView can have a lot of memory, typically by reusing the cell: by specifying a reuse identifier (reuseidentifier) for each cell, which specifies the kind of cell, when the cell rolls out of the screen, The cells that are scrolled out of the screen are placed in the reused queue, and the cells are removed from the queue for reuse when a cell is not displayed on the screen.
But for a mutable custom cell, sometimes this reuse mechanism goes wrong. For example, when a cell contains a subclass of Uitextfield and is placed in the reuse queue for reuse, if a cell that does not contain any child views is to be displayed on the screen, it will be taken out and displayed in a cell with no child views, using the cell that is reused. It's going to go wrong.
The code is as follows
Three methods of each beneficial end,
Method1 The method to get the cell from-(uitableviewcell*) Dequeuereusablecellwithidentifier: (nsstring*) identifier for-(UITableViewCell * Cellforrowatindexpath: (Nsindexpath *) Indexpath reuse mechanism call is dequeuereusablecellwithidentifier this method, means " DEQUEUE a reusable cell, so as long as it is swapped for Cellforrowatindexpath (removing the cell only from the row of the cell being updated), the reuse mechanism is not used, so the problem can be solved, although some space may be wasted。 Example code:-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {static NSString *cellidentifier =@ "Cell";//UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];//change to the following methodUITableViewCell *cell = [TableView Cellforrowatindexpath:indexpath];//Take a row exactly according to the Indexpath, instead of removing it from the cell reuse queue if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Cellidentifier]; }//... Other Code}Method2 is addressed by specifying a different reuse identifier (reuseidentifier) for each cell. The reuse mechanism is to reuse cells based on the same identifiers, and cells with different identifiers cannot be reused by each other. So we can avoid the problem of different cell reuse by setting the identifier of each cell to be different. Example code: [Plain]-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { NSString *cellidentifier = [NSString stringWithFormat:@ "cell%d%d", [Indexpath section], [Indexpath row];//With Indexpath to uniquely determine cellUITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];//DEQUEUE a reusable cell if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Cellidentifier]; }//... Other Code}Method3 Delete All child views of the cell reuse This method removes all child views of the reused cell, resulting in a cell that has no special format for reuse by other cells. Example code:[plain]-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { static NSString *cellidentifier = @ "Cell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; //DEQUEUE The reusable cell if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle: Uitableviewcellstyledefault Reuseidentifier:cellidentifier]; } Else { //Delete all child views of cell while ([cell.contentView.subviews lastobject]! = nil) {[(uiview*) [ Cell.contentView.subviews Lastobject] Removefromsuperview]; } } //... Other code }
Tableviewcontroller cell Reuse Error in iOS (content is garbled)