Paper tail ----- UIKit basics-UITableView, end of uitableview list
I. Basic Introduction
In iOS, the most common way to display table data is to use UITableView. UITableView inherits from UIScrollView. Therefore, it supports vertical scrolling and provides excellent performance.
UITableView has two styles: UITableViewStylePlain and UITableViewStyleGrouped. There is no essential difference between the two operations, but the latter is displayed by group style, and the former is displayed by normal style. Let's take a look at the two applications:
Ii. UItableview data display process
♥UITableView requires a data source to display data.
♥UITableView queries the total number of rows of data from the data source and the data displayed on each row.
♥UITableView with no data source set is just an empty shell
♥Any OC object that complies with the UITableViewDataSource protocol can be a UITableView data source.
♥Data presentation process:
(1) How many groups of data are obtained by calling the grouping method below the data source?
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView;
(2) call the following explain method of the data source to know how many rows of data each group has.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section;
(3) tune the lower limit Limit Method of the region data source to know what each region displays
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath;
Note: UItableview is UITableViewStylePlain by default. You can set style on the right.
3. built-in UITableViewCell
1. Structure of UITableViewCell
2. Reuse Principle of cell
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, you 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 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 creating new objects
There is another very important issue: Sometimes you need to customize UITableViewCell (using a subclass to inherit UITableViewCell), and 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. Therefore, when UITableView is used to reuse UITableViewCell, The UITableViewCell of the error type may be obtained.
Solution: UITableViewCell has an NSString * reuseIdentifier attribute. You can input a specific string identifier when initializing UITableViewCell to set the reuseIdentifier (generally the class name of UITableViewCell is used ). 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.
Iv. cell reuse code
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// 1. define the identifier of a cell static NSString * ID = @ "mjcell"; // 2. retrieve cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ID] From the cache pool; // 3. if no cell in the cache pool if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: ID];} // 4. set cell attributes... return cell;