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: