UITableView 实现自定义 TableViewCell

创建一个继承 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
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。