【问题标题】:UITableView not displaying the correct number of rowsUITableView 未显示正确的行数
【发布时间】:2017-03-26 17:04:26
【问题描述】:

我的 cellForRow 在索引路径函数中存在问题,我很难发现它。所以我转向这个机构的好人。 我执行一个获取请求,该请求返回 Floors 类型的结果数组(这是我的实体)。我在表中只有一个部分,结果只有一行。如果我声明 5 个楼层,我只会得到一行文本值为“4”。它始终是我声明的价值 - 1. 有人可以看看我的代码并帮助我发现问题吗?谢谢你和亲切的问候。

class AssignNumberOfRoomsForFloorsVC: UITableViewController {

//MARK: - Properties

private var managedObjectContext: NSManagedObjectContext!

private var storedFloors = [Floors]()


//MARK: - Actions

override func viewDidLoad() {
    super.viewDidLoad()
    managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    loadFloorData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

private func loadFloorData() {
    let request: NSFetchRequest<Floors> = Floors.fetchRequest()
    request.returnsObjectsAsFaults = false
    do {
        storedFloors = try managedObjectContext.fetch(request)
        self.tableView.reloadData()
    }
    catch {
        print("could not load data from core \(error.localizedDescription)")

    }
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "floor cell", for: indexPath) as! FloorCell
    let floorItem = storedFloors[indexPath.row]
    cell.floorNumberTxt.text = String(floorItem.floorNumber)
    return cell
}

此代码用于设置楼层数:

 @IBAction private func setTheNumberOfFloors(_ sender: UIButton) {
    let house = NSEntityDescription.insertNewObject(forEntityName: "House", into: managedObjectContext) as! House
    house.numberOfFloors = floorNumberValue
    print(" the number of floors in the house is: \(house)")

    loadFloorData()
    for floor in storedFloors {
        for i in 0...floor.numberOfFloors - 1 {
            let instance = NSEntityDescription.insertNewObject(forEntityName: "Floors", into: managedObjectContext) as! Floors
            instance.floorNumber = i
            print("\(instance)")
            house.addToFloors(instance)
        }
    }
 private func loadFloorData() {
    let request = NSFetchRequest<House>(entityName: "House")
    request.returnsObjectsAsFaults = false
    do {
        storedFloors = try managedObjectContext.fetch(request)
    }
    catch {
        print("could not load data from core \(error.localizedDescription)")

    }
}

【问题讨论】:

  • 这一切都取决于这一行:storedFloors = try managedObjectContext.fetch(request)。如果这给出了错误的结果,那么您需要找出原因。
  • 谢谢。我可以看到打印中创建的 Floors 对象的数量,这是正确的。我用来设置楼层数的内容在编辑后的问题部分代码列表中
  • 那里肯定有问题,但我无法发现。如果你不介意看看,请
  • 我部分修复了它。我只是将实体描述 init 放在循环中。我确实得到了正确的行数,但顺序不正确。对于 3 层楼,我得到 0 2 1。谁能告诉我为什么会这样?谢谢
  • 你能不能试着把重载调用 self.tableView.reloadData() 从 loadFloorData() 移过来,然后在 loadFloorData() 调用之后放入 viewDidLoad。

标签: swift uitableview core-data swift3 nsfetchrequest


【解决方案1】:

正如 Fahim 所说,您需要为用于显示表格的 fetch 设置一个排序描述符:

request.sortDescriptors = [NSSortDescriptor(key: #keyPath(Floors.floorNumber), ascending: true)]

另外,请确保在插入新实体或进行任何修改后保存 moc:

        do {
            try managedObjectContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }

或者,如果您使用的是标准生成代码:

UIApplication.shared.delegate.saveContext()

【讨论】:

    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多