iOS development UITableView Performance Application tips Tableviewcell reuse

Source: Internet
Author: User

iOS devices have limited memory, and if you use UITableView to display thousands of data, you need thousands of UITableViewCell objects, which will deplete the memory of your iOS device. To work around this problem, you need to reuse the UITableViewCell object
?
The principle of reuse: when scrolling through a list, some UITableViewCell are moved out of the window, UITableView the UITableViewCell outside the window into an object pool for reuse. When UITableView requires DataSource to return UITableViewCell, DataSource will look at the object pool first, if there are unused uitableviewcell in the pool, DataSource will configure the UITableViewCell with the new data and return it to the UITableView and back to the window to avoid creating new objects

? There is also a very important question: Sometimes you need to customize UITableViewCell (inherit UITableViewCell with a subclass), and each row is not necessarily the same uitableviewcell, So a uitableview may have different types of uitableviewcell, and there will be many different types of UITableViewCell in the object pool, Then UITableView may get the wrong type when reusing UITableViewCell UITableViewCell
?
Solution: UITableViewCell has a nsstring *reuseidentifier property that allows you to set reuseidentifier by passing in a specific string identifier when initializing UITableViewCell ( Generally used UITableViewCell class name). When UITableView requires DataSource to return UITableViewCell, a string is first identified to the object pool to find the corresponding type of UITableViewCell object, if there is, reuse, if not, The string identifier is passed in to initialize a UITableViewCell object

Example:

The first way: (Not in the way of re-use----not recommended)

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{        *cell = [[UITableViewCell alloc] init];         return cell;}

Second way: (using the way of reuse-----recommended)

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{         static@ "dentifier";         *cell = [TableView dequeuereusablecellwithidentifier:dentifier];         if (cell = = nil)            {= [[UITableViewCell alloc]  initwithstyle:uitableviewcellstylesubtitle  Reuseidentifier:dentifier];    }        return  cell}

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.