UITableView 实现自定义 TableViewCell

2019-05-27  本文已影响0人  ImWiki

创建一个继承 UITableViewCell 的类

image.png

选择UITableViewController 的 Cell,关联刚刚创建的 UserCell。

image.png

关联 Label 到 UserCell

image.png

为Cell设置Identifier

image.png

实现代码

class HomeTimelineViewController: UITableViewController {

    let users = ["Wiki","Teny","Joy"]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "userCell"
        let cell = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? UserCell
        let item = users[indexPath.row]
        cell?.userLabel.text = item
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }
}
上一篇 下一篇

猜你喜欢

热点阅读