您的位置:首页 > 移动开发 > IOS开发

iOS项目开发实战——学会使用TableView列表控件(三)了解Section

2015-08-23 20:20 741 查看
     在列表控件TableView中,Section可以用来分隔不同功能的Cell,如下的iPhone设置界面就是用了Section。现在我们要自己来实现一下带Section的TableView。




(1)关于如何设置界面以及拖拉控件,请参考我的前面2篇博客《iOS项目开发实战——学会使用TableView列表控件(一)》《iOS项目开发实战——学会使用TableView列表控件(二)》。

(2)在代码中实现如下:

import UIKit

class 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: NSIndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
var title = cell.viewWithTag(101) as! UILabel

title.text = array[indexPath.row]

return cell

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2    //设置有2个Section;
}

func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {

var str:String!
if(section == 0){

str = "页脚:第一个section"
}else{

str = "页脚:第二个section"
}

return str
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

var str:String!
if(section == 0){

str = "页眉:第一个section"
}else{

str = "页眉:第二个section"
}

return str
}

}


(3)运行程序,实现效果如下:


.

github主页:https://github.com/chenyufeng1991  。欢迎大家访问!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: