UITableView simple performance optimization, uitableview

Source: Internet
Author: User

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;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.