使用swift创建tableview
- ViewController
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
private var tableView : UITableView? = nil
private var listArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
// 加载数据
addData()
// 添加tableview
addTableview()
}
}
extension ViewController {
private func addTableview () {
tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height), style: .plain)
tableView?.delegate = self
tableView?.dataSource = self
view.addSubview(tableView!)
// 需要注册 cell
tableView?.register(MainCell.self, forCellReuseIdentifier: "Identifier")
}
}
// MARK -- delegate
extension ViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 180
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Identifier = "Identifier"
var cell : MainCell = tableView.dequeueReusableCell(withIdentifier: Identifier, for: indexPath) as! MainCell
if cell.isEqual(nil) {
cell = UITableViewCell(style: .default, reuseIdentifier: Identifier) as! MainCell
}
let dic = Dictionary<String, String>()
cell.setValueForCell(dic: dic as NSDictionary)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let nav = CollectionVC()
navigationController?.pushViewController(nav, animated: true)
}
}
// MARK -- addData
extension ViewController {
private func addData() {
listData { (listA) in
// 拼接数据
self.listArray += listA
// 刷新列表
self.tableView?.reloadData()
}
}
}
// MARK -- listData
extension ViewController {
private func listData (completHander: @escaping(_ lists:[String]) -> ()) {
var a = [NSString]()
DispatchQueue.global().async {
for i in 0..<20 {
a.append(NSString(string: "\(i)"))
}
DispatchQueue.main.async {
completHander(a as [String])
}
}
}
}
- MainCell
import UIKit
class MainCell: UITableViewCell {
// 定义属性
private var showImage: UIImageView?
private var fristTitle : UILabel?
private var subTitle : UILabel?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
}
extension MainCell {
private func setupUI () {
showImage = UIImageView.init(frame: CGRect(x: 15, y: 15, width: 150, height: 150))
showImage?.layer.masksToBounds = true
self.contentView.addSubview(showImage!)
subTitle = UILabel.init(frame: CGRect(x: 180, y: 10, width: 100, height: 12))
subTitle?.font = UIFont.systemFont(ofSize: 15)
subTitle?.textAlignment = .center
subTitle?.textColor = UIColor.red
self.contentView.addSubview(subTitle!)
fristTitle = UILabel.init(frame: CGRect(x: 180, y: 50, width: 100, height: 30))
fristTitle?.font = UIFont.systemFont(ofSize: 18)
fristTitle?.textAlignment = .center
fristTitle?.textColor = UIColor.blue
self.contentView.addSubview(fristTitle!)
}
func setValueForCell(dic : NSDictionary) {
subTitle?.text = "45到"
fristTitle?.text = "打牌的"
showImage?.image = UIImage(named: "head_2@3x")
}
}
使用纯代码创建tableview