UITableView simple performance optimization, uitableview
The memory of iOS devices is limited. If you use UITableView to display thousands of data records, you need thousands of UITableViewCell objects, which consumes the memory of iOS devices. To solve this problem, we need to reuse the UITableViewCell Object reuse principle: when the list is rolled, some uitableviewcells will be removed from the window, And UITableView will put the UITableViewCell outside the window into an object pool, waiting for reuse. When UITableView requires dataSource to return UITableViewCell, dataSource will first view this object pool. If there is an unused UITableViewCell in the pool, dataSource will configure the UITableViewCell with new data and then return it to UITableView, re-display in the window to avoid the important problem of creating new objects: Sometimes you need to customize UITableViewCell (use a subclass to inherit UITableViewCell ), in addition, each row does not necessarily use the same UITableViewCell. Therefore, a UITableView may have different types of UITableViewCell, and there are many different types of UITableViewCell in the object pool, when UITableView is used to reuse UITableViewCell, The UITableViewCell of the wrong type may be obtained. Solution: UITableViewCell has N SString * reuseIdentifier attribute. You can input a specific string identifier when initializing UITableViewCell to set the reuseIdentifier (generally the class name of UITableViewCell ). When UITableView requires dataSource to return UITableViewCell, it first uses a string to identify the corresponding type of UITableViewCell object in the object pool. If yes, It is reused. If no, this string identifier is passed in to initialize a UITableViewCell object code:
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
// 1. Define the ID of a cell
Static NSString * ID = @ "Mycell ";
// 2. Retrieve the cell from the cache pool
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ID];
// 3. If no cell exists in the cache pool
If (cell = nil ){
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: ID];
}
// 4. Set cell attributes...
Return cell;
}