Whether beginners, or veteran, as long as the learning of iOS people know how important tableview for development, and then we are using TableView, you may encounter a variety of problems, such as today I want to say the Tableviewcell reuse of the problem:
As we all know, in TableView return each row of the data source method of the cell, we generally use the cell to achieve the purpose of saving memory: 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 cache pool, and the cells are removed from the cache pool for reuse when a cell is not displayed on the screen.
But for a mutable custom cell, sometimes this reuse mechanism goes wrong. Let me give you a simple example, look at the two pictures below.
<1> normal display of pictures
<2> image of the interface disorder
By comparing the two pictures, it is obvious that the interface of the second picture shows confusion, for the simple reason that there is a problem when reusing the cell, then how can we solve the interface confusion or other problems? Below I one by one for everyone to introduce:
Method 1:
The method to get the cell from-(uitableviewcell*) Dequeuereusablecellwithidentifier: (nsstring*) identifier for-(UITableViewCell *) Cellforrowatindexpath: (Nsindexpath *) Indexpath
The reuse mechanism calls the Dequeuereusablecellwithidentifier method, which means "dequeue a reusable Cell", So just swap it for cellforrowatindexpath (remove the cell only from the row of the cell you want to update), you can use no reuse mechanism, so the problem can be solved, although some space may be wasted.
Here is the sample code:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
static NSString *cellidentifier = @ "Cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; Change to the following method
UITableViewCell *cell = [TableView Cellforrowatindexpath:indexpath]; Take a row exactly as Indexpath, rather than removing it from the cell reuse queue
if (cell = = nil) {
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];
}......
The above code is undoubtedly able to solve the problem of interface confusion, but if there is a lot of data, it will waste quite a lot of space, not recommended to use.
Method 2:
Specify a different reuse identifier (reuseidentifier) for each cell to resolve.
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.
How do you set different identifiers for each cell? It's very simple. In the data source method that returns each row of cells by binding Indexpath.section--indexpath.row for each cell identifier, here is the sample code:
Implementing the specific content of cell creation
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
Static NSString * ID = [NSString stringwithformat:@ "cell-ld%-ld%", [Indexpath section],[indexpath Row]];
2. Go to the cache pool to find reusable identifiers
Hmweibocell * cell = [TableView dequeuereusablecellwithidentifier:id];
if (cell = = nil) {
cell = [[Hmweibocell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];
}
Assigning a property framemodel to a cell
Hmweiboframemodel * Framemodel = Self.weiboframearray[indexpath.row];
Cell.weiboframemodel = Framemodel;
return cell;
}
Although the above method has some improvement over the first way, by binding a different identifier for each cell, the cell and cell will not be reused, which solves the problem of interface confusion, but fundamentally there is not much memory optimization. Similarly, if the data is more or more wasted space.
Method 3:
The third way is simply to delete all the sub-views of cell reuse, what does that mean? When we get the reused cell from the cache pool, we get a cell with no special formatting by removing all the child views of the reused cell for reuse by other cells. If you don't know what that means, then we'll go on to the code:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
static NSString *id = @ "Cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id]; DEQUEUE a reusable cell
if (cell = = nil) {
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];
}
Else
{
Delete all child views of a cell
while ([Cell.contentView.subviews lastobject]! = nil)
{
[(uiview*) [cell.contentView.subviews Lastobject] removefromsuperview];//force reload to UIView type, remove all child views
}
}
return cell;
}
Read the above code is not feel this way is very new, this way compared to the first two ways is more perfect, not only to achieve the purpose of reuse, and will not lead to the interface confusion and other problems, if you have encountered a custom cell when the interface is not so bad, you may try this way, you will feel very magical ...
Method 4:
After reading the previous three ways, do you think there is no after the text, you are wrong, below I would like to say the fourth way:
This fourth way is actually a rather stupid method, why so, we can think of, since the reuse of the cell caused the interface confusion because the data is not replaced when the cell is reused, reuse the data of the cell is reused together, then why do we unexpectedly when the custom cell setting data By judging what data each cell should use, if the data used by each cell is different, then there is no problem with the interface data or frame, so I'll use a simple example to show you:
If it is a member of a certain software, then the member icon frame settings if not the member then reset so that can solve the member icon data and frame issues
if (WEIBOMODEL.VIP) {//If it is a member
CGFloat vipx = Cgrectgetmaxx (_NAMEF) + kmargin;
_VIPF = CGRectMake (vipx, Kmargin, Kvipwidth, kvipwidth);
} else{
_VIPF = Cgrectzero;
}
Another example
Set the frame of the picture to be aware of whether there is a picture
if (weibomodel.picture.length>0) {//have picture
CGFloat Picturey = Cgrectgetmaxy (_TEXTF) + kmargin;
_picturef = CGRectMake (Kmargin, Picturey, Kpicturewidth, kpicturewidth);
If there is a picture row height is the largest picture of the Y value plus a margin
Self.cellheight = Cgrectgetmaxy (_picturef) + kmargin;
}else {
_picturef = Cgrectzero;
If there is no picture the largest row height is the maximum height of the body plus a height
Self.cellheight = Cgrectgetmaxy (_TEXTF) + kmargin;
The purpose of the above code is to set a frame to the picture when there is no picture so we set the frame to 0, so that we can easily solve the problem of interface confusion caused by frame.
The above four ways, have their own humble opinion, but also learn from the online Daniel method, I hope this article can help you, what questions can leave a message, I will sparing, liker ....
iOS development solves the problem of interface confusion caused by reusing Tableviewcell