import UIKit
//定义数据源
class TVData: NSObject, UITableViewDataSource {
//返回Sections个数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//取得cell
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
//设置cell中的Label类型
var label = cell.viewWithTag(1) as UILabel
label.text = "Hello " + String(indexPath.row)
var button = cell.viewWithTag(9) as UIButton
//设置Cell中的Button类型
println(button.layer.position)
button.frame = CGRectMake(button.layer.position.x, button.layer.position.y, 100, 30)
button.setTitle("Click Me", forState: UIControlState.Normal)
button.setTitle("Go \(indexPath.row)", forState: UIControlState.Highlighted)
//返回cell对象
return cell
}
//返回行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
//加载Section的表头
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//延时执行
NSThread.sleepForTimeInterval(1)
let date = NSDate()
let formatter = NSDateFormatter()
formatter.timeStyle = NSDateFormatterStyle.FullStyle
let result = formatter.stringFromDate(date)
return result
}
}
//TableView的类对象
class MyTV: UITableView {
//定义数据源
var tvData = TVData()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//初期化时为TabelView设置数据源
self.dataSource = tvData
}
}