闲话不多说
首先初始化一个tableView
fileprivate var tableView :UITableView = {
//和OC的代码其实区别不是很大
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width:mScreenWidth, height:mScreenHeight), style: .plain) //.plain .grouped 分组与不分组
tableView.rowHeight = 50 //设置row的高度
tableView.backgroundColor = UIColor.green //背景颜色
tableView.showsVerticalScrollIndicator = false
tableView.showsHorizontalScrollIndicator = false //是否显示水平和垂直滚动条
tableView.separatorStyle = .none
tableView.register(UITableViewCell.self, forCellReuseIdentifier:tabelViewCellID)//注册tableViewCell
return tableView;
}()
可以看出和OC代码区别并不是很大,唯一的区别是delegate和dataSource的设置不能再此处设置 注册tableViewCell也是和OC一样,在之前现给Identitfier标识 let tabelViewCellID = "tableViewCell"
设置代理
fileprivate func setupUI(){
//设置tableView 的代理
tableView.delegate = self
tableView.dataSource = self
//添加到View上
self.view .addSubview(tableView)
}
在viewDidLoad()方法里面设置代理并添加到self.view上,当然,我这里是ViewController,如果是TableViewController就另当别论了
override func viewDidLoad() {
super.viewDidLoad()
//ui布局
setupUI()
}
然后是添加代理方法,因为我是ViewController,所以要手动添加
extension ViewController :UITableViewDataSource,UITableViewDelegate {
//tableView Sections
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
//row
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10;
}
//cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: tabelViewCellID, for: indexPath)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: tabelViewCellID)
}
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
虽然样子改变了,但是熟悉OC代码的小伙伴应该也是一眼就能明白。
运行结果
这样简单的tableView就做好了。
下面是完整代码
import UIKit
let tabelViewCellID = "tableViewCell"
class ViewController: UIViewController {
fileprivate var tableView :UITableView = {
//和OC的代码其实区别不是很大
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width:mScreenWidth, height:mScreenHeight), style: .plain) //.plain .grouped 分组与不分组
tableView.rowHeight = 50 //设置row的高度
tableView.backgroundColor = UIColor.green //背景颜色
tableView.showsVerticalScrollIndicator = false
tableView.showsHorizontalScrollIndicator = false //是否显示水平和垂直滚动条
tableView.separatorStyle = .none
tableView.register(UITableViewCell.self, forCellReuseIdentifier:tabelViewCellID)//注册tableViewCell
return tableView;
}()
override func viewDidLoad() {
super.viewDidLoad()
//ui布局
setupUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController{
fileprivate func setupUI(){
//设置tableView 的代理
tableView.delegate = self
tableView.dataSource = self
//添加到View上
self.view .addSubview(tableView)
}
}
extension ViewController :UITableViewDataSource,UITableViewDelegate {
//tableView Sections
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
//row
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10;
}
//cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: tabelViewCellID, for: indexPath)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: tabelViewCellID)
}
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}