简体   繁体   中英

An empty cell on tableView + CoreData

Can someone tell me why it don't return cell for me? I really don't now why. On " cellTableViewCell " I have only outlets with label's. Or I need to read at 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
}
}

I think you should set UITableViewDelegate like this : class tabViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

and in viewDidLoad:

tableView.delegate = self
tableView.dataSource = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM