//
// ViewController.swift
// 013-tableView
//
// Created by 庄壮勇 on 2018/1/4.
// Copyright © 2018年 Personal. All rights reserved.
//
import UIKit
class ViewController: UIViewController , 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" , for: indexPath)
// 提示: textLabel 是可选的
// 代码中 ? 是自动带的, 如果textLabel 有,就使用, 如果没有,就什么也不做
cell. textLabel ?. text = "hello ---- \ ( indexPath. row )"
return cell
}
override func viewDidLoad() {
super . viewDidLoad ()
setupUI ()
}
func setupUI() {
// 1. 创建表格
let tv = UITableView (frame: view . bounds , style: . plain )
// 2. 添加到视图
view . addSubview (tv)
// 3. 注册可重用 cell [UITableView class]
tv. register ( UITableViewCell . self , forCellReuseIdentifier: "cellId" )
// 4. 设置数据源
// Swift 中没有遵守协议是一个错误
tv. dataSource = self
}
}
// ViewController.swift
// 013-tableView
//
// Created by 庄壮勇 on 2018/1/4.
// Copyright © 2018年 Personal. All rights reserved.
//
import UIKit
class ViewController: UIViewController , 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" , for: indexPath)
// 提示: textLabel 是可选的
// 代码中 ? 是自动带的, 如果textLabel 有,就使用, 如果没有,就什么也不做
cell. textLabel ?. text = "hello ---- \ ( indexPath. row )"
return cell
}
override func viewDidLoad() {
super . viewDidLoad ()
setupUI ()
}
func setupUI() {
// 1. 创建表格
let tv = UITableView (frame: view . bounds , style: . plain )
// 2. 添加到视图
view . addSubview (tv)
// 3. 注册可重用 cell [UITableView class]
tv. register ( UITableViewCell . self , forCellReuseIdentifier: "cellId" )
// 4. 设置数据源
// Swift 中没有遵守协议是一个错误
tv. dataSource = self
}
}