提问者:小点点

UITableViewCell公共函数未执行


var list = [String]()
@IBOutlet weak var TableView: UITableView!

override func viewDidLoad() {
    self.title = "Routines"
    TableView.delegate = self
    TableView.dataSource = self
    super.viewDidLoad()
}
//refresh view when going back to this viewcontroller
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("Test Worked")
    TableView.reloadData()
}
//generating rows
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return (list.count)
}

//returning text in UITableViewCell
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = UITableViewCell(style:
            UITableViewCell.CellStyle.default, reuseIdentifier:
        "prototype1")
        print("printed")
        cell.textLabel?.text = list[indexPath.row]
        return cell
}
//deleting rows
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
    {
        if editingStyle == UITableViewCell.EditingStyle.delete{
            deleteAllData("ToDo")
            self.list.remove(at: indexPath.row)
            TableView.reloadData()
    }
    
}
@IBAction func didAdd() {
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(identifier: "addRoutinePage")as! addRoutinePage
    self.navigationController?.pushViewController(vc, animated: true)

}

//function to get data from core data
func getData()
{
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "ToDo")
    request.returnsObjectsAsFaults = false
    do{
        //fetching data from coredata
        let result = try context.fetch(request)
        for data in result as! [NSManagedObject]
        {
            //appending the list from the value in coredata (attribute) or entity
            self.list.append(data.value(forKey: "title")as! String)
            print("append success")
        }
    }catch {
        print("failed")
    }
}

我的代码有什么问题? 除了UITableViewCell之外,其他一切似乎都能正常工作,我输入的print命令只是为了检查函数是否被执行,它甚至都不能正常工作。 我尝试了TableView.reloadData(),但它仍然不工作。 从逻辑上讲,如果问题出在公共函数,数据源或委托上,它甚至不会生成任何行,但会生成行。 我也尝试调整单元格的高度大小,但它仍然不工作。 请救命!


共1个答案

匿名用户

代码中有几个错误:

  1. CoreData获取数据完成后,您需要重新加载。
func getData() {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "ToDo")
    request.returnsObjectsAsFaults = false
    do {
        let result = try context.fetch(request)
        for data in result as! [NSManagedObject] {
            self.list.append(data.value(forKey: "title")as! String)
        }
        self.TableView.reloadData()
    } catch {
        print(error)
    }
}
override func viewDidLoad() {
    super.viewDidLoad()
    title = "Routines"
    TableView.delegate = self
    TableView.dataSource = self
    getData()
}