【问题标题】:An empty cell on tableView + CoreDatatableView + CoreData 上的空单元格
【发布时间】:2017-02-16 13:47:17
【问题描述】:

谁能告诉我为什么它不为我返回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
}
}

【问题讨论】:

  • wczytaj 什么时候被调用?
  • 如果您在 cellForRowAt 中设置断点,那么数组中的值是您所期望的吗?是您所期望的标签网点吗?
  • @JonRose 它是空的,如何从请求中获取价值?所有查看这里-> link
  • 您在表格视图中在哪里调用reloadData()?请注意,wartosc2 仅返回 one 字符串(数组中键 nazwa 的最后一个值)

标签: ios swift uitableview core-data


【解决方案1】:

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

在 viewDidLoad 中:

tableView.delegate = self
tableView.dataSource = self

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多