テーブルセルを設定するためのカスタムクラスを作成するのは、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
そしてconfigureSelectedCategoryCell()
は、受信機ののViewController拡張に呼び出されます。
extension CreateRecipeVC: RecipeCategoryTableVCDelegate {
func categoryController(controller: RecipeCategoryTableVC, didSelectCategory category: String) {
selectedCategory = category
self.navigationController?.popViewControllerAnimated(true)
let cell = RecipeCategoryCell()
cell.configureSelectedCategoryCell(selectedCategory)
}
}
あなたは本当にあなたが選択したカテゴリはnilオブジェクトではありませんか? – Johnykutty
コンポーネントがロードされてIBOutletに接続される前に、この関数を呼び出すことは可能ですか? – ryantxr
いつ 'configureSelectedCategoryCell'を呼び、どのメカニズム(NSNotification、ダイレクトコールなど)を呼びますか? – Manuel