IOS project development practice-learn to use the TableView list control (3) Understand Section

Source: Internet
Author: User

IOS project development practice-learn to use the TableView list control (3) Understand Section

In the list control TableView, Section can be used to separate cells with different functions. Section is used in the following iPhone settings interface. Now we need to implement TableView with Section by ourselves.

.

 

(1) For details about how to set the interface and drag controls, refer to the previous two blogs "iOS project development practices-learn to use TableView list controls (1) iOS project development practices-learn to use TableView list controls (2).

(2) implement the following in the Code:

 

Import UIKitclass ViewController: UIViewController, UITableViewDataSource {var array = [Hello, iOS, Swift] override func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view, typically from a nib .} func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {return 3} func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIn DexPath)-> UITableViewCell {var cell = tableView. dequeueReusableCellWithIdentifier (cell)! UITableViewCell var title = cell. viewWithTag (101)! UILabel title. text = array [indexPath. row] return cell} func numberOfSectionsInTableView (tableView: UITableView)-> Int {return 2 // two sections are set;} func tableView (tableView: UITableView, titleForFooterInSection Section section: Int) -> String? {Var str: String! If (section = 0) {str = footer: first section} else {str = footer: Second section} return str} func tableView (tableView: UITableView, titleForHeaderInSection section: int)-> String? {Var str: String! If (section = 0) {str = header: first section} else {str = header: Second section} return str }}

(3) run the program to achieve the following results:

 

 

Related Article

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.