- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用了我在网络上流行的核心数据教程之一中找到的代码:
lazy var fetchedResultsController: NSFetchedResultsController = {
let medicinesFetchRequest = NSFetchRequest(entityName: "Medicine")
let primarySortDescriptor = NSSortDescriptor(key: "name.order", ascending: true)
medicinesFetchRequest.sortDescriptors = [primarySortDescriptor]
let frc = NSFetchedResultsController(
fetchRequest: medicinesFetchRequest,
managedObjectContext: self.managedObjectContext!,
sectionNameKeyPath: "name",
cacheName: nil)
frc.delegate = self
return frc
}()
然后我修改tableView的委托(delegate)方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: DrugCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! DrugCell
var medicine = fetchedResultsController.objectAtIndexPath(indexPath) as! Medicine
cell.drugName.text = medicine.name
return cell
}
结果是我的 TableView 中充满了具有相同名称的完全相同的实体,而不是相应行中的每个实体。
我尝试了其他仅使用 fetchRequest 而不使用 fetchResultsController 的方法,这实际上对我有用,但由于重新加载表和其他内容而出现问题,我想 fetchedResultsController 是填充 tableView 时所需的模式。
编辑:
弄明白了,我按如下方式实现了 tableView 数据源方法,它起作用了。
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if let sections = fetchedResultsController.sections {
return sections.count
}
return 0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController.sections {
let currentSection = sections[section] as! NSFetchedResultsSectionInfo
return currentSection.numberOfObjects
}
return 0
}
无论如何,我很高兴知道为什么这个特定的实现可以工作?在我刚刚在 numberOfRowsInSection 方法中返回 costant(用于测试)之前,我还没有实现 NumberOfSectionsInTableView。像这样
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
最佳答案
不确定这是否是导致问题的原因,但看起来您忘记在“tableview: cellForRowAtIndexpath:”数据源方法中返回单元格。
在您的单元格配置代码之后添加一个返回单元格
。
关于ios - fetchedResultsController 不断在每一行中显示相同的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682219/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!