当单元格插入表格时,自定义表格视图单元格内的图像视图消失

问题描述:

这是我第一次处理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!) 
    } 
} 
+0

您是否尝试调试configureCell方法并检查该对象是否存在? – Doro

+0

所以事实证明,插入单元格时,图像视图以及说明和日期的标签就会消失。这是一个约束问题吗? –

这似乎是一个框架问题,因为不仅图像查看两个其他标签的日期和其他标签也缺少,你是否添加约束?

+0

哇,我很专注于imageView没有显示,我没有意识到这些标签丢失。我确实对电池有限制,这可能是问题吗? –

+0

因此,在调查了约束条件之后,我发现问题在于尺寸级别。我切换回任何x任何和布局问题已解决! –

+0

@ C.Koelemay太棒了 –

您共享我可以建议下面的代码:

检查configureCell方法,好像它是那段时间为零。 我已经从你的第一个截图中得出了这样的结论:而不是文本,你的storyboard/xib中指定了占位符。

所以,检查是否

let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject 

从取控制器返回正确的对象(更新它,如果事实并非如此)。

希望这会有所帮助。

+0

所以我调试了configueCell方法并且对象确实存在,但是在遍历函数后,这些错误在控制台中显示出来:2016-08-01 14:14:50.664 Scrapper [4654:503178] _BSMachError:(os/kern)无效能力(20) 2016-08-01 14:14:50.664 Scrapper [4654:503178] _BSMachError:(os/kern)无效名称(15)是否与这些bug有关? –