import UIKit
class ViewController: UIViewController {
lazy var tableView:UITableView = UITableView();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.setupUI()
}
}
//MARK: - 更页面设置相关 -
extension ViewController
{
func setupUI() {
// 0.将tableView添加到控制器的View中
self.view.addSubview(tableView)
// 1.设置tableView的frame
tableView.frame = self.view.bounds;
// 2.设置数据源
tableView.dataSource = self;
// 3.设置代理
tableView.delegate = self;
tableView.backgroundColor = UIColor.orange
}
}
//MARK: - 数据源和代理方法 -
extension ViewController : UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//创建cell
let cellID = "CellID"
var cell = tableView.dequeueReusableCell(withIdentifier: cellID)
if cell == nil {
cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellID)
}
cell?.textLabel?.text = "数据源:\(indexPath.row)"
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("ss选择了\(indexPath.row)行")
}
}
网友评论