The end of the love paper-----iOS learning UITableView

Source: Internet
Author: User

First, the basic introduction

In iOS, for tabular data presentation, the most common practice is to use Uitableview,uitableview to inherit from Uiscrollview, so vertical scrolling is supported, and performance is excellent.

There are two styles of UITableView: Uitableviewstyleplain and uitableviewstylegrouped. In fact, there is no essential difference between the two, but the latter is shown by the grouping style, the former is shown in Normal style. Let's look at the application of both:

Second, the process of UITableView display data

♥uitableview requires a data source (DataSource) to display the data
♥uitableview will query the data source for the total number of rows of data and what data each row displays.

♥ No data source is set UITableView is just an empty shell.

♥ Any OC object that complies with the Uitableviewdatasource protocol can be a UITableView data source

♥ The process of presenting data:

(1) Call the following in the data source? How many sets of data are there

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView;

(2) Call the following method of the data source how many rows of data are in each group
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section;

(3) next to the data source? The law learns every?? What to show

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath;

Note: UITableView is uitableviewstyleplain by default, you can set the style on the right

Third, the UITableViewCell of their own

1, the structure of UITableViewCell

2. Cell re-use principle

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

♥ Re -use principle: when scrolling through the list, some UITableViewCell will move out of the window, and UITableView will place 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

Iv. Reusing code for cells

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    //1. Defines the identity of a cell      static nsstring *id = @ "Mjcell";        2. Remove the cell      uitableviewcell *cell = [TableView Dequeuereusablecellwithidentifier:id] from the cache pool;        3. If the buffer pool does not have a cell      if (cell = = nil) {        cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id];    }        4. Set the properties          of the cell ... return cell;

  

The end of the love paper-----iOS learning UITableView

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.