when using UITableView, because of the cell reuse mechanism, when retrieving background data and populating the cell, there will be cell duplication, interface disorder. But this only happens with multiple sections, and when there is no scrolling, the row of a single section is displayed correctly.
The following is the sample code:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath//This method is the DataSource method of UITableView, which is used to load/reuse the cell. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *simpletableidentifier =@"Simpletableidentifier"; //UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:simpletableidentifier];UITableViewCell *cell =[TableView Cellforrowatindexpath:indexpath]; if(Cell = =Nil) { //... your codeCell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifi Er:simpletableidentifier]; } returncell;}
The variables in this tableviewcontroller will appear every time they are created :
1. Simpletableidentifier: The ID of the cell, the Dequeuereusablecellwithidentifier method will be used. In the Alloc cell , the Simpletableidentifier is passed in and assigned to the Reuseidentifier property of the cell.
。 If you use Dequeuereusablecellwithidentifier to get the cell, make sure that the cell that each indexpath.row points to Simpletableidentifier is the only one.
2. Dequeuereusablecellwithidentifier:: This method returns the same cell as the current Simpletableidentifier reuseidentifier , which is why the order is confusing, if not found, returns nil.
3. Cellforrowatindexpath:: This method returns the same cell as the current Indexpath , so the simpletableidentifier is the same for all cells , will also get the correct order, if not found, returns nil.
Summary :
If in your program, if there is a sort of confusion or the same cell content, check whether you are using the Dequeuereusablecellwithidentifier method or the Cellforrowatindexpath method.
Because this is not the case with the Cellforrowatindexpath method, if you are using the Dequeuereusablecellwithidentifier method, make sure that each cell Reuseidentifier is the only one.
but the mechanism of cell reuse cannot be ignored, UITableViewCell has a queue that is dedicated to storing cells that have been generated, but that are later hidden by rolling TableView.
like a table with 20 cells, but the screen can only display 5, (of course iPhone5 may show 6), then there will be other cells are not displayed, but it will appear when you slide the tableview.
And in the code:
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:simpletableidentifier];
is to remove a temporarily unused cell from the queue according to the identifier, only the cell is nil, that is, when there is no old cell in the queue, will only execute:
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Simpletableidentifier];
This generates a new cell.
If there is an old one, you do not have to execute the statement, so saving resources is counted as a reuse.
There must be no cell in the queue when the TableView is initialized, so each cell will execute the code once it is generated:
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Simpletableidentifier];
when the screen is full and the next line is scrolled up, the first line is hidden and placed in that queue . then the newly added line executes the statement:
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:simpletableidentifier];
this time the result is not nil, and then, skip the statement 2, so save resources.
IOS UITableView when sliding in order or multiple cell contents are the same