我的数据模型
Fetch
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
}
我是新来的核心数据。我已经从cart实体中获取数据了。我的桌子看上去就像截图。看看Cart-user(38)-363690,它显示了一个购物车Id中的两个单元格,因为它有2张照片。如何才能只显示一个单元格,即使它有两个或更多的照片?
发布于 2016-02-02 22:15:05
从这里,我能理解的是,如果有照片的购物车,它应该只显示一行,并显示在单元格中的照片计数。为此,numberOfRowsInSection
方法应该只返回1。不需要返回图像数。在cellForRowAtIndexPath
方法中,根据部分中的照片数量配置行。
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
}
https://stackoverflow.com/questions/35168805
复制相似问题