Recycling of UITableViewCell -- for beginners and uitableviewcell --
The reuse mechanism of UITableViewCell effectively saves memory overhead and improves program performance.
1 Principle
TableView has a cache pool that stores cells that are not in use (not displayed on the interface.
When a cell row is displayed in tableView, it is first found in the cache pool. If no cell row is displayed, it is created. If a cell row is hidden, it is placed in the cache pool.
2. Circular exploitation code
// CellForRow code
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
Static NSString * ID = @ "test"; // cell cycle utilization ID is "test"
// Find the cell marked as "test" from the cache pool corresponding to the current tableView;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ID];
// If no cell exists in the cache pool, that is, the cell is empty and a new cell is created.
If (! Cell ){
Cell = [[UITableViewCell alloc] init];
}
Return cell;
}
3. Recycling problem 3.1 Problem 1
Here we introduce a data model LSUser (user model ),
Attributes: name (NSString, name), vip (BOOL, member or not)
Figure 3-1
// Set data in the cellForRow Method
// Set the user name
Cell. textLabel. text = user. name;
// If the user is a vip, set the user name to red.
If (user. vip ){
Cell. textLabel. textColor = [UIColor redColor];
}
Because Wu Ba is not a member, skip the if statement. The color of Wu BA's name should be black, but it actually keeps the red color of the member set by Chen Qi cell0. This is a simple issue of recycling.
If a symmetric setting statement is added to the if statement, this problem will not occur.
If (user. vip ){
Cell. textLabel. textColor = [UIColor redColor];
} Else {
Cell. textLabel. textColor = [UIColor blackColor];
}
The recycling of UITableViewCell requires us to set the view's appearance and status in a symmetric manner.
In fact, the cycle utilization problem of this case can be considered that the color of cell. textLabel. textColor is black by default. Assume that the color of the non-member name is blue, so when setting the data:
Cell. textLabel. textColor = user. vip? [UIColor redColor]: [UIColor blueColor];
Think carefully...
3.2 Question 2
There is a need: click a cell line, the current cell user name color is changed to purple, the remaining is the original black
You may do this:
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
Cell. textLabel. textColor = [UIColor purpleColor];
}
-(Void) tableView :( UITableView *) tableView didDeselectRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
Cell. textLabel. textColor = [UIColor blackColor];
}
For the moment, it seems that the requirements are met. The color of the currently clicked row is purple, and the remaining is black.
However, when you drag tableView, the current row is hidden and dragged randomly. Suddenly, the color of a row is purple, and then you return to the current row you clicked. The name is Black rather than purple.
This is also a problem of recycling. Next, we will solve this problem.
When a cell line is to be displayed, the tableView data source method-tableView: cellForRowAtIndexPath will be called;
Circular exploitation affects cell display and does not affect raw data. In this method, data settings are performed to introduce two solutions:
1) loop utilization does not affect indexPath, and indexPaht is unique.
First, you have a selectedIndexPath attribute of the NSIndexPath type, which is used to record the currently selected rows and assign values in the didSelectRowAtIndexPath method.
Then in the-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath method,
// Set Data
// Retrieve the data model of the corresponding row
LSUser * user = self. users [indexpath. row];
// Set the user name
Cell. textLabel. text = user. name;
// Set the name color based on whether the row is selected
If (self. selectedIndexPath = indexPath ){
Cell. textLabel. textColor = [UIColor purpleColor];
} Else {
Cell. textLabel. textColor = [UIColor blackColor];
}
2) start with the data, derive a data model dedicated to the cell from the data model, append the corresponding attributes, and then process and set the data in the corresponding place. I will not go into details here. This solution is suitable for handling complex situations. For example, if it is not necessarily selected or not selected, it may be three or more States, or the cell animation effect, or you may need [tableView reloadData.