我以前做的swift笔记, 之前都是整理在onenote上, 最近想到整理出博客. 也方便自己查找, 可以当做自己的一份文档.
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
//1创建
let tv = UITableView(frame: view.bounds, style: .plain)
//2添加
view.addSubview(tv)
//3注册可重用
tv.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
//4设置数据源
tv.dataSource = self
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
cell?.textLabel?.text = "hello ~~~~ \(indexPath.row)"
return cell!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}