2016-08-01 103 views
1

這是我第一次處理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

這似乎是一個框架問題,因爲不僅圖像查看兩個其他標籤的日期和其他標籤也缺少,你是否添加約束?

+0

哇,我很專注於imageView沒有顯示,我沒有意識到這些標籤丟失。我確實對電池有限制,這可能是問題嗎? –

+0

因此,在調查了約束條件之後,我發現問題在於尺寸級別。我切換回任何x任何和佈局問題已解決! –

+0

@ C.Koelemay太棒了 –

0

您共享我可以建議下面的代碼:

檢查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有關? –

相關問題