TableView创建
let table:UITableView = UITableView(frame: CGRect(x: 0, y:0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height), style:.Plain)
UITableViewStyle两种风格
1.UITableViewStyle.Plain
2.UITableViewStyle.Grouped
UITableViewDataSource, UITableViewDelegate实现
//代理设置
table.delegate = self
table.dataSource = self
//行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
//段数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 20
}
//行高
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 30
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cellID"
var cell = tableView.dequeueReusableCellWithIdentifier(identifier)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: identifier)
}
//UITableViewCellStyle
//Default、Value1、Value2、Subtitle
cell!.backgroundColor = UIColor.blueColor()
cell!.textLabel!.text = "haha"
return cell!
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "header"
}
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "footer"
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
网友评论