UITableView Use summary of iOS development

Source: Internet
Author: User
<span id="Label3"></p><strong><strong> <!--StartFragment--> <span style="font-size: 16px; color: #000000;">What is UITableView</span> </strong></strong> <ol> <ol> <li>See a wide variety of tabular data in a wide range of mobile applications</li> <li class="_mce_tagged_br">In ios, the most common practice for tabular data presentation is to use the UITableView</li> <li class="_mce_tagged_br">UITableView inherits from uiscrollview, so vertical scrolling is supported and performance is excellent</li> </ol> </ol><p class="_mce_tagged_br"><p class="_mce_tagged_br"></p></p><p class="_mce_tagged_br"><p class="_mce_tagged_br"> <!--StartFragment--><span style="font-size: 16px;"><strong>Two styles of UITableView</strong></span> <!--EndFragment--></p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li>Uitableviewstyleplain</li> </ul></li> </ul> </ul><p><p></p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li>uitableviewstylegrouped</li> </ul></li> </ul> </ul><p><p></p></p><p><p> <!--StartFragment--><span style="font-size: 16px;"><strong>TableView the process of presenting data</strong></span> <!--EndFragment--></p></p>1. Call the data source the following method to know how many sets of data<pre><pre>-(nsinteger) numberofsectionsintableview: (uitableview *) tableView;</pre></pre>2. Call the following method of the data source to know how many rows of data each group has<pre><pre>-(nsinteger) tableView: (uitableview *) tableView numberofrowsinsection: (nsinteger) section;</pre></pre>3. Call the following method of the data source to know what each row shows<pre><pre>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) indexpath;</pre></pre><p><p> <!--StartFragment--><span style="font-size: 16px;"><strong>About cell</strong></span> <!--EndFragment--></p></p> <ol> <ol> <li>Each line of UITableView is a uitableviewcell, initialized by Datasource's Tableview:cellforrowatindexpath: method to initialize each row</li> <li>There is a default child view inside the Uitableviewcell: Contentview,contentview is the parent view of the content displayed by uitableviewcell, which displays some auxiliary indicator VIEWS. The function of the secondary indicator view is to display an icon representing the action, which can be displayed by setting the Accessorytype of the uitableviewcell, which is uitableviewcellaccessorynone by default (does not display the secondary indicator view), and the other values are as Follows:</li> <li>You can also customize the secondary indicator view via the Cell's Accessoryview property (for example, put a switch to the Right)</li> </ol> </ol><p><p> <!--StartFragment--><span style="font-size: 16px;"><strong>Uitableviewcell's Contentview</strong></span> <!--EndFragment--></p></p>Contentview has 3 sub-views by default, 2 of which are Uilabel (accessed through Uitableviewcell's Textlabel and Detailtextlabel properties), and the 3rd is Uiimageview ( Accessed via Uitableviewcell's ImageView property). UITableViewCell also has a Uitableviewcellstyle property that determines which child views of Contentview are used and where they are located in Contentview. Lists the value of the Uitableviewcellstyle and the display style: <!--StartFragment--><span style="font-size: 16px;"><span style="font-size: 16px;"><strong>UITableViewCell structure Diagram</strong></span></span> <!--EndFragment--> <!--StartFragment--><span style="font-size: 16px;"><span style="font-size: 16px;"><strong>the principle of cell reuse</strong></span></span> <!--EndFragment-->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 resolve this problem, you need to reuse the UITableViewCell Object.<span style="color: #ff0000;"><span style="color: #ff0000;">principle of Reuse</span></span>:<span style="text-decoration: underline; color: #ff0000;"><span style="text-decoration: underline; color: #ff0000;">when the list is scrolled, some UITableViewCell are moved out of the window, and 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. </span></span>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<span style="color: #ff0000;"><span style="color: #ff0000;">Solution</span> solutions</span>:<span style="text-decoration: underline;"><span style="text-decoration: underline;"><span style="color: #ff0000; text-decoration: underline;">UITableViewCell has a nsstring *reuseidentifier property that allows you to set reuseidentifier by passing in a specific string identifier when the UITableViewCell is initialized ( 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</span></span></span> <!--StartFragment--><span style="font-size: 16px;"><span style="font-size: 16px;"><strong>reuse code for cell</strong></span></span> <!--EndFragment--><pre>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *<span style="color: #000000;"><span style="color: #000000;">) indexpath{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Define the identity of a cell</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>NSString *id =<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Mjcell</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Remove the cell from the cache pool</span></span>UITableViewCell *cell =<span style="color: #000000;"><span style="color: #000000;">[tableView dequeuereusablecellwithidentifier:id]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. If there is no cell in the cache pool</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(cell = =<span style="color: #000000;"><span style="color: #000000;">Nil) {cell</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id]; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">4. Set the properties of the cell</span> ...</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">cell;}</span></span></pre><p><p><span style="font-size: 16px;"><strong><strong>Custom cell</strong></strong></span></p></p> <ul> <ul> <li>To customize the cell by Xib the way you customize the view</li> </ul> </ul>1. Create a new Xib file describing the internal structure of a view (assumed to be called Hoocell.xib) 2. Create a custom class (the custom class needs to inherit from the System's own UITableViewCell or subclass, inheriting from which class, depending on the xib root Object's class) 3. The class name of the new class is best aligned with the Xib file name (for example, The class name is Hoocell) 4. Connect the controls in Xib and The. m file of the custom class 5. Provides a class method to return a created custom view (masking process from Xib Loading) 6. provide a model Properties let the outside Pass model data 7. Override the setter method of the model property, where the model data is presented to the corresponding child controls <ul> <ul> <li>To customize a cell by code (MVVM)</li> </ul> </ul><p><p>1. Create a new class that inherits from UITableViewCell</p></p><p><p>2. Overriding initwithstyle:reuseidentifier: Methods</p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li>Add all the child controls that need to be displayed (you don't need to set the child Control's data and frame, and the child controls are added to the Contentview)</li> </ul></li> </ul></li> </ul> </ul> <ul> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li>Make child controls one-time property settings (some properties only need to be set once, such as fonts \ fixed Pictures)</li> </ul></li> </ul></li> </ul> </ul><p><p>3.2 Models Available</p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li>Data model: storing text data \ Picture data</li> <li>Frame Model: The height of the Frame\cell that holds the data model \ All child controls</li> </ul></li> </ul></li> </ul> </ul><p><p>4.cell has a frame model (don't have a data model directly)</p></p><p><p>5. Override the setter method of the frame model property: set the child Control's display data and frame in this method</p></p><p><p>The initialization of the 6.FRAME model data has been done in a lazy manner (each cell corresponds to the frame model data is loaded only Once)</p></p> <!--EndFragment--><p><p>UITableView Use summary of iOS development</p></p></span>

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.