2.swift3.0-创建TableView

一: 创建TableView

swift和oc创建tableview基本上一模一样.

1.遵守tableView的代理.

一开始遵循这两个代理后xcode会报错.那是因为你没实现代理方法.后面实现了.这个error就消失了.

class GXPhoneMainHomeVC: GXPhoneBaseVC, UITableViewDelegate, UITableViewDataSource

2.创建tableView

2.1先声明tableview变量

var tableView = UITableView()

2.2设置tableview的属性

private func configureTableView() {
    self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 44), style: UITableViewStyle.plain)
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.self))
    self.tableView.showsVerticalScrollIndicator = false
    self.tableView.showsHorizontalScrollIndicator = false
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none // 取消每个cell底部的分割线
}

3.实现tableview的代理方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 30
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self), for: indexPath)
    cell.textLabel?.text = "敬请期待!"
    cell.selectionStyle = UITableViewCellSelectionStyle.none
    return cell
}

你可能感兴趣的:(2.swift3.0-创建TableView)