import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {//表格视图数据源协议、表格视图代理协议
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRect(x: 0, y: 40, width: 320, height: 420)//创建一个现实区域
let tableView = UITableView(frame: rect)//初始化一个表格视图,并设置其位置和尺寸
tableView.delegate = self//设置表格视图的代理为当前的视图控制器类
tableView.dataSource = self//设置表格视图的数据源为当前的视图控制器类
self.view.addSubview(tableView)//将表格视图添加到当前视图控制器的根视图中
}
//添加一个代理方法,用来设置表格视图y拥有5行单元格
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
//添加一个代理方法,用来初始化或复用表格视图中的单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
Xcode9学习笔记40 - 使用UITableView制作简单表格
最新推荐文章于 2022-03-13 10:37:30 发布