1.storyboard 添加一个TableView控件
2.控件绑定数据
@IBOutlet weak var tbv: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tbv.dataSource=self
tbv.delegate=self
}
3.必须实现接口方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style:UITableViewCell.CellStyle.subtitle, reuseIdentifier: nil)
cell.textLabel!.text = "Cell te1xt"
cell.detailTextLabel?.text = "Cell Subtitle1"
cell.imageView!.image = UIImage(named:"image1.png")
return cell
}
因此,UITableViewDataSource和 UITableViewDelegate接口实现简单的TableView:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tbv: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tbv.dataSource=self
tbv.delegate=self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style:UITableViewCell.CellStyle.subtitle, reuseIdentifier: nil)
cell.textLabel!.text = "Cell te1xt"
cell.detailTextLabel?.text = "Cell Subtitle1"
cell.imageView!.image = UIImage(named:"image1.png")
return cell
}
}