首页 > 解决方案 > UITableViewCell 滚动后被删除

问题描述

我正在尝试创建一个 ToDo 列表应用程序。对于这个应用程序,我需要一个自定义 UITableViewCell。我正在尝试使用我的自定义单元格,但是,当我运行应用程序并尝试滚动表格视图时,所有单元格都会被删除。

这是我的代码:

class ToDoListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let nib = UINib(nibName: "TodoCell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: "customCell")
        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return HomeVC.ToDoEvents.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! TodoCell
        let eventName = HomeVC.ToDoEvents[indexPath.row].name
        let eventTime = HomeVC.ToDoEvents[indexPath.row].time
        let completion = HomeVC.ToDoEvents[indexPath.row].isComplete

        cell.initialize(task: eventName!, length: eventTime!, completion: completion!)

        return cell

    }

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

我不确定我做错了什么。我经历了其他一些线程,但是,它们都没有解决我的问题。

标签: iosswiftuitableview

解决方案


推荐阅读