這是我第一次處理iOS開發。我正在創建一個應用程序,它使用自定義的UITableViewCell顯示從Core Data中檢索的信息。我遇到這個問題,當單元格插入表格時,顯示顏色的imageView會隨機消失。如果圖像視圖消失,關閉應用程序並重新啓動它將解決問題。當單元格插入表格時,自定義表格視圖單元格內的圖像視圖消失
插入表格後右:pic 力後,關閉應用程序,並重新啓動:pic
在哪裏的問題,將不勝感激任何意見。
這是一些相關的代碼。如果有幫助,我也使用NSFetchedResultsController。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CustomTableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableViewCell
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
self.configureCell(cell, withObject: object)
return cell
}
func configureCell(cell: CustomTableViewCell, withObject object: NSManagedObject) {
let imageData: NSData = object.valueForKey("image") as! NSData
let colorImageBlank: UIImage = UIImage(named: "blank.png")!
let picture: UIImage = UIImage(data: imageData)!
let date: NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
cell.cellImage.image = picture
let colorString = object.valueForKey("color")!.description
var color: UIColor = UIColor()
if colorString == "Blue" {
color = UIColor.blueColor()
} else if colorString == "Red" {
color = UIColor.redColor()
} else if colorString == "Green" {
color = UIColor.greenColor()
} else if colorString == "Yellow" {
color = UIColor.yellowColor()
} else if colorString == "Orange" {
color = UIColor.orangeColor()
} else if colorString == "Purple" {
color = UIColor.purpleColor()
}
cell.colorImage.image = colorImageBlank
cell.colorImage.backgroundColor = color
cell.colorImage.layer.cornerRadius = 5.0
cell.cellImage.layer.borderWidth = 2
cell.cellImage.layer.borderColor = color.CGColor
cell.cellImage.layer.cornerRadius = 5.0
cell.locationNameLabel.text = object.valueForKey("name")!.description
cell.locationNameLabel.font = UIFont.boldSystemFontOfSize(14.0)
cell.locationNameLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail
cell.categoryLabel.text = object.valueForKey("category")!.description
cell.categoryLabel.font = UIFont.italicSystemFontOfSize(12.0)
cell.categoryLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail
date = object.valueForKey("date")! as! NSDate
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
cell.dateLabel.text = dateFormatter.stringFromDate(date)
cell.descriptionLabel.text = object.valueForKey("descript")!.description
cell.descriptionLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)! as! CustomTableViewCell, withObject: anObject as! NSManagedObject)
case .Move:
tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
}
}
您是否嘗試調試configureCell方法並檢查該對象是否存在? – Doro
所以事實證明,插入單元格時,圖像視圖以及說明和日期的標籤就會消失。這是一個約束問題嗎? –