首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义获取核心数据(迅速)

自定义获取核心数据(迅速)
EN

Stack Overflow用户
提问于 2016-02-03 04:57:07
回答 1查看 166关注 0票数 2

我的数据模型

Fetch

代码语言:javascript
运行
复制
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")
    }
}

截图

更新:行数

代码语言:javascript
运行
复制
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
}

我是新来的核心数据。我已经从cart实体中获取数据了。我的桌子看上去就像截图。看看Cart-user(38)-363690,它显示了一个购物车Id中的两个单元格,因为它有2张照片。如何才能只显示一个单元格,即使它有两个或更多的照片?

EN

回答 1

Stack Overflow用户

发布于 2016-02-03 06:15:05

从这里,我能理解的是,如果有照片的购物车,它应该只显示一行,并显示在单元格中的照片计数。为此,numberOfRowsInSection方法应该只返回1。不需要返回图像数。在cellForRowAtIndexPath方法中,根据部分中的照片数量配置行。

代码语言:javascript
运行
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35168805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档