Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 自定义获取核心数据(swift)_Ios_Swift_Core Data_Nsfetchedresultscontroller_Datamodel - Fatal编程技术网

Ios 自定义获取核心数据(swift)

Ios 自定义获取核心数据(swift),ios,swift,core-data,nsfetchedresultscontroller,datamodel,Ios,Swift,Core Data,Nsfetchedresultscontroller,Datamodel,我的数据模型 获取函数 func fetchAndSetResults2(){ let dataController = DataController.sharedController let moc = dataController.managedObjectContext let request = NSFetchRequest(entityName: "Cart") let formatSort = NSSortDescriptor(key: "cartI

我的数据模型

获取函数

func fetchAndSetResults2(){
    let dataController = DataController.sharedController
    let moc = dataController.managedObjectContext
    let request = NSFetchRequest(entityName: "Cart")
    let formatSort = NSSortDescriptor(key: "cartId", ascending: true)
    request.sortDescriptors = [formatSort] //[formatSort, nameSort]
    request.predicate = NSPredicate(format: "cartToUser.userId = %@", serverUserId)


    fetchedResultController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: "cartId", cacheName: nil)
    fetchedResultController.delegate = self

    do {
        try fetchedResultController.performFetch()  
    }
    catch {
        fatalError("Error in fetching records")
    }
}
屏幕截图

更新:行数

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if let sections = fetchedResultController.sections {
        return sections.count
    }
    return 0
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let sections = fetchedResultController.sections {
        let currentSection = sections[section]
        return currentSection.numberOfObjects
    }
    return 0
}

我是核心数据方面的新手。我已经在从购物车实体获取数据。我的表视图看起来像截图。看看购物车用户(38)-363690,它在一个购物车Id中显示了两个单元格,因为它有两张照片。如何在有两张或更多照片的情况下只显示一个单元格?

由此我可以理解的是,如果购物车中有照片,它应该只显示一行,并显示单元格中的照片计数。为此,
numberOfRowsInSection
方法应仅返回1。不需要返回图像的数量。在
cellforrowatinexpath
方法中,根据节中的照片数量配置行

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
     if let sections = fetchedResultController.sections {
        return sections.count
     }
     return 0
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
     let title: String
     if let sections = fetchedResultController.sections {
        let currentSection = sections[indexPath.section]
        title  = "\(currentSection.numberOfObjects) Photos"
     } 
     else {
        title  = "0 Photos"
     }
     //set the label title for photos

    return cell
}

问题可能与NumberOfSection和numberofrowsinsection方法有关。请在此处添加该方法好的,请检查我编辑的问题@JohnykuttyCart user(38)-XXXXX是表格视图单元格的一部分还是它的节标题?我认为它是节标题,因此对于您来说,如果有照片,它应该只显示一行,并显示其中的照片计数正确吗?节,实际上,我使用section只显示一个cartId中有多少单元格。我只想为每个cartId显示一个单元格@谢谢你的回答。它可以工作,但有没有办法做到这一点而不改变部分?我的意思是,我使用section只是为了指导我,并计划稍后隐藏sectionTile。我无法获得您需要的全部内容。因此,基于我的理解,这是我保证的方法