2016-08-01 15 views
1

これは初めてiOS開発を扱うときです。私はカスタムUITableViewCellを使用してコアデータから取得した情報を表示するアプリケーションを作成しています。 Imがこの問題を抱えているのは、セルがテーブルに挿入されたときに色を表示するimageViewがランダムに消えてしまうことです。画像ビューが表示されない場合は、アプリを終了して再起動すると問題が解決されます。右の表の中に挿入した後セルがテーブルに挿入されると、カスタムテーブルビューのセル内のイメージビューが表示されなくなる

:力はアプリを閉じて再起動した後picpic

問題がどこにあるか上の任意のアドバイスをいただければ幸いです。

関連するコードの一部です。私は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

画像ビューだけでなく、日付と他のラベルの他の2つのラベルも欠落しているので、フレーミングの問題と思われます。制約を追加していますか?

+0

うわー、私はイメージに焦点を当てていました。私がそれらのラベルが欠落していたことに気付かなかったことを示していません。私は細胞に制約がありますが、それらが問題になる可能性はありますか? –

+0

制約を調べた後、問題はサイズクラスであることがわかりました。私はAny Any Anyに戻って、レイアウトの問題は解決されました! –

+0

@ C.Koelemay great –

0

は、あなたのconfigureCell方法を確認し、それはその期間にnilだように思えます。 私はあなたの最初のスクリーンショットからそのような結論を出しました。テキストの代わりに、あなたのストーリーボード/ 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)これらはバグに関連していますか? –

関連する問題