2016-03-30 4 views
2

私は素早く初心者で、私はカスタムを試してみますUITableViewCell、私はYouTubeとインターネットで多くのチュートリアルを参照してください。しかし、私はそれを行うことはできません、私は多くの方法を修正しようとしたが、何も変化はありません。カスタムでUITableViewCellをスピーディに行う方法

class movieViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"] 


@IBOutlet weak var tableView: UITableView! 
@IBOutlet weak var btnMenu: UIBarButtonItem! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    btnMenu.target = self.revealViewController() 
    btnMenu.action = Selector("revealToggle:") 

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
    self.revealViewController().rearViewRevealWidth = 200 
    self.tableView.registerClass(CategoryRow.self, forCellReuseIdentifier: "Cell") 

    // Do any additional setup after loading the view. 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return categories[section] 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return categories.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CategoryRow 
    cell.labelA.text = self.categories[indexPath.row] 
    cell.labelB.text = self.categories[indexPath.row] 

    return cell 
} 

CategoryRow.swift:

class CategoryRow: UITableViewCell { 
    var labelA: UILabel! 
    var labelB: UILabel! 

    override func awakeFromNib() { 

     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

と私のバグ:あなたはlabelAまたはlabelBを割り当てていない

enter image description here

+0

どこで 'labelA'と' labelB'を作成しますか – Abizern

答えて

3

xibがないようです。そしてあなたはあなたのラベルを宣言するだけですが、あなたはそれを初期化しません。従ってlabelAlabelBはゼロになります。それはクラッシュする。

あなたはXIBをしたくない場合、あなたは以下のコードのようにCategoryRowで2つの機能を実装する必要があります。もう一つの方法は、あなたのクラスと同じ名前でXIB UITableCiewCellを作成することです

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 

} 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    self.labelA = UILabel(frame: CGRectMake(0,0, self.contentView.frame.size.width, self.contentView.frame.size.height)) 
//Same here for labelB 
    self.contentView.addSubview(labelA) 
} 

。このセルをあなたのクラスに設定します。このxibにlabelAlabelBを設定し、コンセントをクラスにドラッグします。そしてあなたはで修正するviewDidLoad

self.tableView.registerNib(UINib(nibName: "nameYourxib"), forCellReuseIdentifier: "Cell") 
+0

あなたは私を救っています。もし私が.xibを持っていたらどうですか? –

+0

@EvanNgo編集の回答を確認してください。 –

+1

あなたはベトナム人ですか? –

1

は、ここに私のコードです。それはなぜエラーを表示するのですか?あなたは

viewDidLoad() { 
    self.tableView.delegate = self 
    self.tableView.datasource = self 
} 

のviewDidLoad

にデリゲートにデータソースを追加する必要が enter image description here

-3

、あなたはコンセントない変数によってlabelAとlabelBを作成する必要があります:あなたはこのようなUIを使用してラベルを接続します。コントロールを保持し、ストーリーボードから対応するファイルにドラッグして、コンセントを選択し、名前を付けますlabelAとlabelB

+0

デリゲートが設定されていない場合、デリゲートメソッドは呼び出されません。 'viewDidLoad'メソッドをオーバーライドし、' super.viewDidLoad() 'を呼び出さないと、バグに噛まれてしまいます。ラベルの部分は正しいです。 – redent84

0

@vienvu答えは正しいですが、XIBなしで何をするのですか? Hereは即座にカスタムセルの完全なソリューションです

関連する問題