Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速-获取一对多数据并将其显示在tableview中_Ios_Swift_Core Data - Fatal编程技术网

Ios 快速-获取一对多数据并将其显示在tableview中

Ios 快速-获取一对多数据并将其显示在tableview中,ios,swift,core-data,Ios,Swift,Core Data,我有两个实体: 游泳池 参数 游泳池与参数有一对多关系(每个游泳池有多个参数)。我也有一个相反的关系 我已经保存了我的数据,我提取了游泳池以显示参数 现在我尝试在tableview中显示它们。这是我的密码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(w

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

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

我已经保存了我的数据,我提取了游泳池以显示参数

现在我尝试在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 attemptFetch(){
let fetchRequest:NSFetchRequest=SwimmingPool.fetchRequest()
让dataSort=NSSortDescriptor(键:“swimminPoolCreatedAt”,升序:false)
fetchRequest.sortDescriptors=[dataSort]
让controller=NSFetchedResultsController(fetchRequest:fetchRequest,managedObjectContext:context,sectionNameKeyPath:nil,cacheName:nil)
controller.delegate=self
self.controller=控制器
做{
尝试controller.performFetch()
}抓住{
let error=错误为N错误
打印(“\(error.debugDescription)”)
}
}//结束尝试获取
以下是我如何管理不同的案例:

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
    }
}
func控制器(controller:NSFetchedResultsController,didChange anObject:Any,在indexPath:indexPath?处,对于类型:NSFetchedResultsChangeType,newIndexPath:indexPath?){
开关(类型){
案例.插入:
如果让indexPath=newIndexPath{
tableView.insertRows(位于:[indexPath],带:.fade)
}
打破
case.delete:
如果让indexPath=indexPath{
tableView.deleteRows(位于:[indexPath],带:.fade)
}
打破
case.update:
如果让indexPath=indexPath{
将cell=tableView.cellForRow(at:indexPath)设为!ParamCell
configureCell(单元格:单元格,indexPath:indexPath作为NSIndexPath)
}
打破
case.move:
如果让indexPath=indexPath{
tableView.deleteRows(位于:[indexPath],带:.fade)
}
如果让indexPath=newIndexPath{
tableView.insertRows(位于:[indexPath],带:.fade)
}
打破
}
}
我的问题是,该表仅随机显示一个结果(我插入了3个参数)


有什么想法吗?

您正在用以下内容覆盖每个参数。就CoreData而言,表视图是一对一视图。要显示所有参数,请执行到详图视图控制器的步骤,传递游泳池实例并使用关联参数填充详图数据源数组。或者使用不同的部分,每个部分一个pool@vadian这实际上是我应该使用segue来显示detailsview的表。。。我正在对游泳池做同样的事情,而且它工作得非常好。。我不确定是否理解您的建议…但我可以看到paramArray给了我3个值。。。然后当它进入for循环时。。。它只返回一个值…然后控制器应该包含参数数组。请添加
控制器
是如何填充的代码?@vadian我有这个var控制器:NSFetchedResultsController!我不明白。这是主视图控制器还是局部视图控制器?您正在用以下内容覆盖每个参数。就CoreData而言,表视图是一对一视图。要显示所有参数,请执行到详图视图控制器的步骤,传递游泳池实例并使用关联参数填充详图数据源数组。或者使用不同的部分,每个部分一个pool@vadian这实际上是我应该使用segue来显示detailsview的表。。。我正在对游泳池做同样的事情,而且它工作得非常好。。我不确定是否理解您的建议…但我可以看到paramArray给了我3个值。。。然后当它进入for循环时。。。它只返回一个值…然后控制器应该包含参数数组。请添加
控制器
是如何填充的代码?@vadian我有这个var控制器:NSFetchedResultsController!我不明白。这是主视图控制器还是局部视图控制器?