tableView + CoreData上的空单元格

时间:2017-02-16 13:47:18

标签: ios swift uitableview core-data

有人可以告诉我为什么它不会为我返回cell吗?我现在真的不知道为什么。在“cellTableViewCell”我只有标签的出口。 或者我需要阅读func viewWillAppear

var nazwaTab:[String] = []
func getContext() -> NSManagedObjectContext {
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    return (appDelegate?.persistentContainer.viewContext)!
}
func wczytaj(wartosc:String) -> String {
    var wartosc2 = wartosc
    let request: NSFetchRequest<Ostatnie> = Ostatnie.fetchRequest() 
    do {
        let result = try getContext().fetch(request)
        for liczby in result {
            if let str = liczby.value(forKey: "nazwa") as? String
            {
                wartosc2 = str
                nazwaTab.append(wartosc2)
                nazwaTab.last
            }
            print(nazwaTab)
        }
    } catch {
        print(error)
    }
    return wartosc2
}
}
extension tabViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nazwaTab.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cellTableViewCell
    cell.dlugoscLabel.text = dlugoscTab[indexPath.row] as! String
    cell.nazwaLabel.text = nazwaTab[indexPath.row]
    cell.szerokoscLabel.text = szerokoscTab[indexPath.row] as! String
    return cell
}
}

1 个答案:

答案 0 :(得分:0)

我认为你应该像这样设置UITableViewDelegate:  class tabViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

并在viewDidLoad中:

tableView.delegate = self
tableView.dataSource = self
相关问题