Dies ist das erste Mal, dass ich mich mit der iOS-Entwicklung beschäftige. Ich erstelle eine App, die eine benutzerdefinierte UITableViewCell verwendet, um Informationen anzuzeigen, die aus Core Data abgerufen werden. Ich habe dieses Problem, bei dem eine Bildansicht, die eine Farbe anzeigt, zufällig verschwindet, wenn die Zelle in die Tabelle eingefügt wird. Wenn die Bildansicht nicht mehr angezeigt wird, wird das Problem durch Schließen der App und erneutes Starten behoben.Die Bildansicht in der benutzerdefinierten Tabellenansichtszelle verschwindet, wenn die Zelle in die Tabelle eingefügt wird
Gleich nach dem in Tabelle einfügen: pic Nach Schließkraft App und Neustart: pic
Jede Beratung über, wo das Problem würde geschätzt wird.
Hier ist ein Teil des relevanten Codes. Ich benutze auch NSFetchedResultsController, wenn das hilft.
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!)
}
}
Haben Sie versucht, die configureCell-Methode zu debuggen und zu prüfen, ob an diesem Punkt ein Objekt existiert? – Doro
So stellt es sich heraus, dass wenn die Zellen eingefügt werden, die Bildansicht und die Beschriftungen für die Beschreibung und das Datum einfach verschwinden. Ist das ein Einschränkungsproblem? –