import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupUI()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
cell.textLabel?.text="\(indexPath.row)"
return cell
}
func setupUI() {
//初始化table,为了简化代码,frame为全屏尺寸
let table:UITableView = UITableView(frame:view.bounds, style: .plain)
//table添加到视图上,并声明delegate和datasource
self.view!.addSubview(table)
table.dataSource = self
table.delegate = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
}
}
网友评论