iOS Development UITableView Basic Usage Summary 1

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>UITableView Basic Use Method</strong></p></p><p><p>1. first, the controller needs to implement two delegate, namely Uitableviewdelegate and Uitableviewdatasource</p></p><p><p>2. Then the delegate of the UITableView object to be set to Self.</p></p><p><p>3. Then you can implement some of these delegate to Pull.</p></p><p><p><strong>(1)-(nsinteger) numberofsectionsintableview: (uitableview *) tableView;</strong></p></p><p><p>This method returns the number of sections that TableView has</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt"><span class="comment">Returns the number of sections</span></li> <li>-(nsinteger) numberofsectionsintableview: (uitableview *) TableView</li> <li class="alt">{</li> <li><span class="keyword">Return 1;</span></li> <li class="alt">}</li> </ol> </ol><p><p><strong>(2)-(nsinteger) tableView: (uitableview *) table numberofrowsinsection: (nsinteger) section;</strong></p></p><p><p>This method returns the number of elements in the corresponding section, which is the number of Rows.</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(nsinteger) tableView: (uitableview *) tableView numberofrowsinsection: (nsinteger) section</li> <li>{</li> <li class="alt"><span class="keyword">Return 10;</span></li> <li>}</li> </ol> </ol><p align="left"><p align="left"><strong>(3)-(cgfloat) tableView: (uitableview *) tableView heightforrowatindexpath: (nsindexpath*) indexpath;</strong></p></p><p><p>This method returns the height of the specified row.</p></p><p><p>-(cgfloat) tableView: (uitableview *) tableView heightforheaderinsection: (nsinteger) section;</p></p><p><p>This method returns the height of the header view for the specified Section.</p></p><p><p>-(cgfloat) tableView: (uitableview *) tableView heightforfooterinsection: (nsinteger) section;</p></p><p><p>This method returns the height of the footer view for the specified Section.</p></p> <ol class="dp-cpp"> <li class="alt"><li class="alt">-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) Indexpath</li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">Static NSString * Showuserinfocellidentifier = @<span class="string">"showuserinfocell"; </span></span></li></li> <li><li>UITableViewCell * cell = [tableview_ dequeuereusablecellwithidentifier:showuserinfocellidentifier];</li></li> <li class="alt"><li class="alt"><span class="keyword">if (cell = = Nil)</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="comment">Create a cell to the display an Ingredient.</span></li></li> <li><li>Cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle</li></li> <li class="alt"><li class="alt">reuseidentifier:showuserinfocellidentifier]</li></li> <li><li>autorelease];</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">Configure the Cell.</span></li></li> <li><li>[email protected]  <span class="string">"signature"; </span></li></li> <li class="alt"><li class="alt">Cell.detailTextLabel.text = [nsstring stringWithCString:userInfo.user_signature.c_str () encoding: nsutf8stringencoding];</li></li> <li><li>}</li></li> </ol><p><p><strong>(5)-(cgfloat) tableView: (uitableview *) tableView heightforheaderinsection: (nsinteger) section</strong></p></p><p><p>Returns the height of the header of the specified section</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(cgfloat) tableView: (uitableview *) tableView heightforheaderinsection: (nsinteger) section</li> <li>{</li> <li class="alt"><span class="keyword">If (section ==0)</span></li> <li><span class="keyword">Return 80.0f;</span></li> <li class="alt"><span class="keyword">Else</span></li> <li><span class="keyword">Return 30.0f;</span></li> <li class="alt">}</li> </ol> </ol><p><p><strong>(6)-(nsstring *) tableView: (uitableview *) tableView titleforheaderinsection: (nsinteger) section</strong></p></p><p><p>Returns the title of the header of the specified section, and if the section header has a return view, the title will not Work.</p></p> <ol class="dp-cpp"> <li class="alt"><li class="alt">-(nsstring *) tableView: (uitableview *) tableView titleforheaderinsection: (nsinteger) section</li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">if (tableView = = Tableview_)</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">if (section = = 0)</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">return @<span class="string">"title 1"; </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">else <span class="keyword">if (section = = 1)</span></span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">return @<span class="string">"title 2"; </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">Else</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">Return nil;</span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li><span class="keyword">Else</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="keyword">Return nil;</span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> </ol><p><p><strong>(7)-(UIView *) tableView: (uitableview *) tableView viewforheaderinsection: (nsinteger) section</strong></p></p><p><p>Returns the view of the specified section header, and if not, the function can not return the view</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(UIView *) tableView: (uitableview *) tableView viewforheaderinsection: (nsinteger) section</li> <li>{</li> <li class="alt"><span class="keyword">if (section = = 0)</span></li> <li>{</li> <li class="alt"></li> <li>uiview* Header = [[[nsbundle mainbundle] loadnibnamed: @<span class="string">"settingheaderview"</span></li> <li class="alt">Owner:self</li> <li>options:nil] lastobject];</li> <li class="alt"></li> <li><span class="keyword">Else</span></li> <li class="alt">{</li> <li><span class="keyword">Return nil;</span></li> <li class="alt">}</li> <li>}</li> </ol> </ol><p><p><strong>(8)-(void) tableView: (uitableview *) tableView didselectrowatindexpath: (nsindexpath *) Indexpath</strong></p></p><p><p>When the user selects a cell in a row, the callback uses This. But first, you must set a property of TableView to select Only.</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">tableview.allowsselection=yes;</li> </ol> </ol> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">cell.selectionstyle=uitableviewcellselectionstyleblue;</li> </ol> </ol><p><p>If you do not want to respond to select, you can set the property with the following code:</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">tableview.allowsselection=no;</li> </ol> </ol><p><p>Here is the response to the select click function, according to which section, which row will respond to ITSELF.</p></p> <ol class="dp-cpp"> <li class="alt"><li class="alt">-(<span class="keyword">void) tableView: (uitableview *) tableView didselectrowatindexpath: (nsindexpath *) indexpath</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">if (indexpath.section = = 1)</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">Return</span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">else <span class="keyword">if (indexpath.section==0)</span></span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">Switch (indexpath.row)</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="comment">Chat</span></li></li> <li><li><span class="keyword">Case 0:</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li>[self ontalktofriendbtn];</li></li> <li class="alt"><li class="alt">}</li></li> <li><li><span class="keyword">Break</span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">Default</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Break</span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li><span class="keyword">Else</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="keyword">return;</span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>How to allow the cell to respond to select, but the selected color does not change, then set</p></p><p><p>Cell.selectionstyle = uitableviewcellselectionstylenone;</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) Indexpath</li> <li>{</li> <li class="alt"><span class="comment">The color of the cell is not changed after it is selected</span></li> <li>Cell.selectionstyle = uitableviewcellselectionstylenone;</li> <li class="alt">}</li> </ol> </ol><p><p><strong>(9) How to set the split line between each line of TableView</strong></p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">self.tableview.separatorstyle=uitableviewcellseparatorstylesingleline;</li> </ol> </ol><p><p>If you do not need to split the line, then set the property to Uitableviewcellseparatorstylenone.</p></p><p><p><strong>(10) How to set the background color of TableView cell</strong></p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) Indexpath</li> <li>{</li> <li class="alt"><span class="comment">Set Background color</span></li> <li>Cell.contentview.backgroundcolor=[uicolor colorwithred:0.957 green:0.957 blue:0.957 alpha:1];</li> <li class="alt">}</li> </ol> </ol><p><p><strong>(one)-(void) tableView: (uitableview *) tableView accessorybuttontappedforrowwithindexpath: (nsindexpath *) Indexpath</strong></p></p><p><p>This function responds, the user clicks the arrow to the right of the cell (if Any)</p></p><p><p><strong>(12) How to set TableView can be edited</strong></p></p><p><p>The first step is to enter edit Mode:</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">[TableView Setediting:yes animated:yes];</li> </ol> </ol><p><p>If you want to exit edit mode, it must be set to No</p></p><p><p>-(uitableviewcelleditingstyle) tableView: (uitableview *) tableView editingstyleforrowatindexpath: (nsindexpath *) Indexpath</p></p><p><p>Returns the type of edit the current cell is performing, and the following code returns the Delete mode</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(uitableviewcelleditingstyle) tableView: (uitableview *) tableView editingstyleforrowatindexpath: (nsindexpath *) Indexpath</li> <li>{</li> <li class="alt"><span class="keyword">Return uitableviewcelleditingstyledelete;</span></li> <li>}</li> </ol> </ol><p><p>-(void) tableView: (uitableview *) Atableview</p></p><p><p>Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle</p></p><p><p>Forrowatindexpath: (nsindexpath *) Indexpath</p></p><p><p>The notification tells the user which cell has been edited, and for the above code, we perform the Delete cell operation inside the Function.</p></p> <ol class="dp-cpp"> <ol class="dp-cpp"> <li class="alt">-(<span class="keyword">void) tableView: (uitableview *) atableview</span></li> <li>Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle</li> <li class="alt">Forrowatindexpath: (nsindexpath *) Indexpath</li> <li>{</li> <li class="alt">[chatarray removeObjectAtIndex:indexPath.row];</li> <li>[chattableview reloaddata];</li> <li class="alt">}</li> </ol> </ol><p><p><strong>(13) How to get a Cell object for a row</strong></p></p><p><p>-(uitableviewcell *) cellforrowatindexpath: (nsindexpath *) indexpath;</p></p><p><p>iOS Development UITableView Basic Usage Summary 1</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.