Swift 使用NSFetchResultController在TableView中创建奇怪的空单元格
我目前正在使用UITableView和NSFetchResultController编写一个应用程序来管理tableview 我必须多次更改谓词。它工作正常,但有时,当tableview重新加载时,某些单元格未被填充 最后,我的tableview显示了示例:1个好单元格,2个空单元格,3个好单元格,1个空单元格。 我的数据库没有损坏,因为当我触摸一个单元格时,应用程序会在数据库中处理好数据 这种虫子很难复制,让我发疯 这是我的密码: 每次我必须更改分类时加载请求的功能Swift 使用NSFetchResultController在TableView中创建奇怪的空单元格,swift,uitableview,nsfetchedresultscontroller,tableviewcell,Swift,Uitableview,Nsfetchedresultscontroller,Tableviewcell,我目前正在使用UITableView和NSFetchResultController编写一个应用程序来管理tableview 我必须多次更改谓词。它工作正常,但有时,当tableview重新加载时,某些单元格未被填充 最后,我的tableview显示了示例:1个好单元格,2个空单元格,3个好单元格,1个空单元格。 我的数据库没有损坏,因为当我触摸一个单元格时,应用程序会在数据库中处理好数据 这种虫子很难复制,让我发疯 这是我的密码: 每次我必须更改分类时加载请求的功能 //On charge l
//On charge la datasource
func loadFetchContoller(categorie: Categorie) {
//On set le fetchRequest
if self.fetchRequest == nil {
self.fetchRequest = NSFetchRequest(entityName: "New")
}
//On tri par date de publication
var sortDescriptor = NSSortDescriptor(key: "published_at", ascending: false)
self.fetchRequest!.sortDescriptors = [sortDescriptor]
//self.fetchRequest!.fetchBatchSize = 20
//On met à jour le predicate
let predicate = NSPredicate(format: "categorie.name = %@", categorie.name)
self.fetchRequest!.predicate = predicate
//Si le fetchResultController n'existe pas, on le crée
self.fetchedResultsController = NSFetchedResultsController(fetchRequest: self.fetchRequest!, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
self.fetchedResultsController?.delegate = self
//On relance le fetchController
var error: NSError?
self.fetchedResultsController?.performFetch(nil)
self.tableViewNews.reloadData()
}
这里是NSFetchedResultsController的功能
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableViewNews.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
self.tableViewNews.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
case .Update:
if let cell = self.tableViewNews.cellForRowAtIndexPath(indexPath!) {
self.tableViewNews.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
}
case .Move:
self.tableViewNews.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
self.tableViewNews.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
case .Delete:
self.tableViewNews.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
default:
return
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableViewNews.endUpdates()
}
最后是处理TableView的函数:
// MARK: - Table view data source
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//On retourne le nombre de données
if let arraySection = self.fetchedResultsController?.sections {
return arraySection[section].numberOfObjects
} else {
return 0
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
//On récupère la new
var new = self.fetchedResultsController?.objectAtIndexPath(indexPath) as! New
//Si on est sur les photos, on retourne tous le temps la même taille
if new.media == "photo" {
return 220.0
}
//Sinon on calcule la hauteur des strings à afficher
else {
//On récupère le texte final de la description
var lengthDescription = new.description_new.stringByConvertingHTMLToPlainText().length()
if lengthDescription > 200 {
lengthDescription = 200
}
//On récupère la description et on regarde la hauteur du texte que l'on va afficher
var description = FBAHelperController.sharedInstance.getDateInText(NSDate(timeIntervalSince1970: new.published_at.doubleValue)) + " - " + (new.description_new.stringByConvertingHTMLToPlainText() as NSString).substringToIndex(lengthDescription) + "..."
var heightTitre = new.title.heightForString(new.title, font: UIFont(name: "HelveticaNeue-Medium", size: 18.0)!, width: UIScreen.mainScreen().bounds.width - 60)
var heightDescription = new.description_new.heightForString(description, font: UIFont(name: "HelveticaNeue-Light", size: 17.0)!, width: UIScreen.mainScreen().bounds.width - 20)
return heightTitre + heightDescription + 95.0
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//On récupère la new
var new = self.fetchedResultsController?.objectAtIndexPath(indexPath) as! New
//CELLULE AVEC PHOTO
if new.media == "photo" {
//On crée la cellule
let cell = tableView.dequeueReusableCellWithIdentifier("FBATeamInfoWithImageTableViewCell", forIndexPath: indexPath) as! FBATeamInfoWithImageTableViewCell
self.configureCell(cell, atIndexPath: indexPath)
return cell
}
//CELLULE SANS PHOTO
else {
//On crée la cellule
let cell = tableView.dequeueReusableCellWithIdentifier("FBATeamInfoWithoutImageTableViewCell", forIndexPath: indexPath) as! FBATeamInfoWithoutImageTableViewCell
self.configureCell(cell, atIndexPath: indexPath)
return cell
}
}
configureCell()是我用来填充单元格的函数(标签、图像视图等)那么…你使用的是Objective-C还是Swift?或者两者都使用?我使用的是Swift。这可能是我的问题的原因吗?那么你为什么要用Objective-C来标记这个问题?因为我接受与Objective-C相关的答案。我认为这可能很有用。但是如果需要,我可以删除Objective-C标志。你能提供一些代码吗你使用Objective-C或Swift?或两者都使用?我使用Swift。这可能是我的问题的原因吗?那么你为什么要用Objective-C标记这个问题?因为我接受与Objective-C相关的答案。我认为这可能有用。但如果需要,我可以删除Objective-C标志。你能提供一些代码吗