Swift tableView分组
//分组
import UIKit
class Test2ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//分几组
func numberOfSections(in tableView:UITableView) ->Int{
return 3
}
//每个分组多少行
func tableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
switch section {
case0:
return 1
case1:
return 2
case2:
return 3
default:
return 0
}
}
//每组标题
func tableView(_tableView:UITableView, titleForHeaderInSection section:Int) ->String?{
return String(section)
}
//每一行的布局
func tableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
let reuseID = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: reuseID)
if cell == nil {
cell =UITableViewCell(style: .default, reuseIdentifier: reuseID)
}
cell?.textLabel?.text=String(indexPath.row)
return cell!
}
override func viewDidLoad() {
super.viewDidLoad()
}
}