Swift - 获取一个到多个数据并在tableview中显示它

时间:2017-07-10 14:26:27

标签: ios swift core-data

我有两个实体: 游泳池 参数

游泳池与参数有一对多的关系(每个游泳池有很多参数)。我也有反比关系

我保存了我的数据并获取了游泳池以显示参数

现在我正试图在tableview中显示它们。这是我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ParamCell", for: indexPath) as! ParamCell
    configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
    return cell
}


 func configureCell(cell: ParamCell, indexPath: NSIndexPath) {
    let swimmingPool = controller.object(at: indexPath as IndexPath)
    let parameter = swimminPool.parameters?.allObjects

    let paramArray = parameter
    for param in paramArray! {
        cell.configureCell(parameter: param as! Parameter)
    }

}

,这是

部分中的行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if let sections = controller.sections {
        let sectionInfo = sections[section]
        if sectionInfo.numberOfObjects > 0 {
            return sectionInfo.numberOfObjects
        } else {
            let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
            noDataLabel.text = "insert a parameter"
            noDataLabel.textColor = UIColor.black
            noDataLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
            noDataLabel.numberOfLines = 2
            noDataLabel.textAlignment = .center
            tableView.backgroundView = noDataLabel
            tableView.separatorStyle = .none
        }
    }
    return 0
}

这是我获取数据的方式:

func attemptFetch() {

    let fetchRequest: NSFetchRequest<SwimmingPool> = SwimmingPool.fetchRequest()

    let dataSort = NSSortDescriptor(key: "swimminPoolCreatedAt", ascending: false)

    fetchRequest.sortDescriptors = [dataSort]


    let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

    controller.delegate = self
    self.controller = controller

    do {
         try controller.performFetch()
    } catch {
        let error = error as NSError
        print("\(error.debugDescription)")
    }
} //End AttemptFetch

这是我如何管理不同的案例:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

    switch(type){
    case.insert:
        if let indexPath = newIndexPath{
            tableView.insertRows(at: [indexPath], with: .fade)
        }
        break

    case.delete:
        if let indexPath = indexPath{
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
        break

    case.update:
        if let indexPath = indexPath{
            let cell = tableView.cellForRow(at: indexPath) as! ParamCell
            configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
        }
        break
    case.move:
        if let indexPath = indexPath{
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
        if let indexPath = newIndexPath{
            tableView.insertRows(at: [indexPath], with: .fade)
        }
        break
    }
}

我的问题是该表只是随机显示一个结果(我插入了3个参数)

有什么想法吗?

0 个答案:

没有答案