Reuse of UITableViewCell

Source: Internet
Author: User

Reuse of UITableViewCellfirst, the cell'sprinciple of reuse:when scrolling through the list, someUITableViewCellwill move out of the window,UITableViewWill be outside the windowUITableViewCellput in an object pool and wait for reuse. WhenUITableViewRequirementsDataSourcereturnUITableViewCellwhen theDataSourcewill look at this object pool first if there are unusedUITableViewCell,DataSourcethis will be configured with the new data.UITableViewCell, and then return toUITableView, and then re-display to the window to avoid creating new objects.

There is also a very important question: Sometimes you need to customizeUITableViewCell (inherit with one subclassUITableViewCell), and each line is not necessarily the sameUITableViewCell, so aUITableViewmay have different types ofUITableViewCell, there will be many different types of objects in the pool .UITableViewCell, thenUITableViewin reusingUITableViewCellmay get the wrong type whenUITableViewCell.

Solution:UITableViewCellhave aNSString *reuseidentifierproperty that can be initialized in theUITableViewCella specific string identifier is passed in to set theReuseidentifier (General useUITableViewCellthe class name). WhenUITableViewRequirementsDataSourcereturnUITableViewCell, a string is first identified to the object pool to find the corresponding type ofUITableViewCellobject, if any, is reused, and if not, the string identifier is passed in to initialize aUITableViewCellobject.

Second, CellThe performance optimization
UITableViewthe default is to load only those that appear on the screen.Cell, not when there was aCellRemove the screen and it will be stored in the cache pool to find.
performance Optimization steps:
Step One: DefineCell(do not need to be created every timeCellidentity, so you need to useStatic,StaticThe identity will only be created for the first time and will not be created in the future. )
Step two: Fetch from the cache poolcell
Step Three: Determine the removalCellis empty and is created manually if it is emptyCell.

three, how to improveTableViewthe Performance
a, Reusecell
we all know that it takes time to apply for memory, especially in a period of time, the frequent application of memory will be very expensive, andTebleviewinCellin most cases the layout is the same, and this time we can improve performance by recycling the reuse mechanism.
b, avoidcontentthe re-layout
try to avoid reusingCelltime, theCellre-layout, the general situation in creatingCell, you willCellThe layout is good.
C, the use of opaqueSubView
in CustomCell, you will add theSubViewsetting to opaque will greatly reduce the number ofViewthe time required to cascade overtime rendering.
D, if convenient, direct reloadSubViewof theDrawRectMethod
If CustomCellWhen you need multiple small elements in the process, it is best to draw directly on multiple items to be displayed instead of adding multipleSubView.
e,TableViewof theDelegatethe method is not necessary, try not to implement
TableViewof theDelegateprovides a number of functions in theCellfurther control of the properties, such as eachCellthe height,Cellwhether it is possible to edit, supportedEditstyle and so on, if not necessary, it is best not to implement these methods because a quick call to these methods can also affect performance.

Reuse of UITableViewCell

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.