2016-03-31 4 views
0

テーブルセルを設定するためのカスタムクラスを作成するのは、iOS開発の標準的な方法です。私はこの練習を私のアプリにも適用しますが、私はそれにいくつか問題があります。Swift 2.1 - カスタムUITableViewCellクラスでIBOutletプロパティがnilである

ViewControllerの2つのテーブルビューで、それぞれのセルが同じカスタムセルクラスを使用しています。 CustomCellClass

import UIKit 

class RecipeCategoryCell: UITableViewCell { 

    @IBOutlet weak var recipeCategoryLabel: UILabel! 
    @IBOutlet weak var chooseCategoryLabel: UILabel! 

    //... 

    func configureCell(recipe: Recipe) { 
     let category = recipe.category as? RecipeCategory 
     recipeCategoryLabel.text = category!.name 
    } 

    func configureNewCell(category: RecipeCategory) { 
     recipeCategoryLabel.text = category.name 
    } 

    // re-configure receiver's viewcontroller cell with selected category value 
    func configureSelectedCategoryCell(selectedCategory: String) {   
     recipeCategoryLabel.text = selectedCategory 
    } 

    // configure all available category names in the sender's viewcontroller 
    func configureChooseCategoryCell(category: RecipeCategory) { 
     chooseCategoryLabel.text = category.name 
    } 
} 

@IBOutlet weak var recipeCategoryLabel: UILabel!

は、受信機のViewControllerに属し、@IBOutlet weak var chooseCategoryLabel: UILabel!は、送信者のViewControllerに属し

私はXcode

fatal error: unexpectedly found nil while unwrapping an Optional value

を言うrecipeCategoryLabel.text = selectedCategoryことを、このラインに第三の機能に問題があります

同じIBOutlet recipeCategoryLabelを使用するが、選択したカテゴリ値を持つReceiver ViewControllerセルを再構成する関数configureSelectedCategoryCell()だけは、この問題はありません。 これはストーリーボードとの接続上の問題ではないと確信しています。

同じIBOutletプロパティが複数の場所で使用されているため、これが起こっているとします。しかし、問題は、同じプロパティを使用してデフォルト値を設定し、選択した値を更新し、同じラベルに以前に保持された値を設定できるということです。

私の説明では分かりませんが、基本的には、送信者のViewControllerから渡された選択されたカテゴリ値で、受信者のViewControllerの表のセルのラベルを設定できません。

UPDATE

enter image description here

そしてconfigureSelectedCategoryCell()は、受信機ののViewController拡張に呼び出されます。

extension CreateRecipeVC: RecipeCategoryTableVCDelegate { 

func categoryController(controller: RecipeCategoryTableVC, didSelectCategory category: String) { 

    selectedCategory = category 

    self.navigationController?.popViewControllerAnimated(true) 

    let cell = RecipeCategoryCell() 
    cell.configureSelectedCategoryCell(selectedCategory) 

} 

}

+0

あなたは本当にあなたが選択したカテゴリはnilオブジェクトではありませんか? – Johnykutty

+1

コンポーネントがロードされてIBOutletに接続される前に、この関数を呼び出すことは可能ですか? – ryantxr

+0

いつ 'configureSelectedCategoryCell'を呼び、どのメカニズム(NSNotification、ダイレクトコールなど)を呼びますか? – Manuel

答えて

1

あなたがしようとすると、セルの出口はポイントでインスタンス化されていませんにアクセスする10。

セルがInterface Builderで設計されていることを理解しているので、セルオブジェクトcellのアウトレットにInterface Builderのコントロールへの参照がありません。表中のその後

func categoryController(controller: RecipeCategoryTableVC, didSelectCategory category: String) { 

    selectedCategory = category 
    self.navigationController?.popViewControllerAnimated(true) 

    tableViewDataSource[selectedItemIndex].selectedCategory = selectedCategory 
    tableView.reloadData() 
} 

を:

カテゴリを選択したら、あなたが

  1. 更新のtableViewのデータソース
  2. リロードのtableViewの項目

べきこのような何かデータソースデリゲートの表示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifer") as! RecipeCategoryCell 
    cell.configureSelectedCategoryCell(tableViewDataSource[indexPath.row].selectedCategory) 
    return cell 
} 

サイドノート:

ストーリーボードに複数のコントロールに1 IBOutletを接続するために合法的です。

予防措置として、configureCellのオプションのアンラッピングを解除することをお勧めします。

func configureCell(recipe: Recipe) { 
    if let category = recipe.category as? RecipeCategory { 
     recipeCategoryLabel.text = category.name 
    } 
} 
+0

ご意見ありがとうございます。 'tableViewDataSource [selectedItemIndex] .selectedCategory = selectedCategory'送信者から選択された項目のインデックスを取得できますが、なぜselectedCategoryが保持しているかを既に知っているときにこの割り当てが必要な理由がわかりません。 –

+0

2つのテーブルビューに関するアプリケーションのロジックを記述できますか?私は1つのテーブルビューがレシピカテゴリを選択することを理解しました。他のテーブルビューとは何ですか?また、2つのテーブルビューのどれが例外を引き起こしますか? – Manuel

+0

これは私の前の質問で、2つのテーブルビューの背後にあるロジックについて説明しています。例外は受信側でスローされます - CreateRecipeViewController http://stackoverflow.com/questions/36322178/swift-2-1-unexpectedly-found-nil-while-unwrapping-an-optional-on-delegate-meth –

関連する問題