Create UITableView programmatically in Swift

前端 未结 8 1303
青春惊慌失措
青春惊慌失措 2020-12-12 12:00

I try to implement UITableView programmatically without use of xib or Storyboards. This is my code:

ViewController.swift

import UIKi         


        
8条回答
  •  既然无缘
    2020-12-12 12:52

    import UIKit
    
    class ViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.register(TableCell.self, forCellReuseIdentifier: "cell")
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableCell
        cell.nameLabel.text = "TableViewCell programtically"
        cell.nameLabel.textAlignment = .center
        cell.nameLabel.textColor  = .white
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    }
    

提交回复
热议问题