2016-06-15 11 views
0

2つの行で構成されたグループ化されたテーブルビューがあり、各行にはカスタムテーブルビューセルがあります。カスタムテーブルビュークラスとセル識別子がすべて確立され、インターフェイスビルダーのテーブルビュー行に関連付けられています。私の最初のtableviewセルは正常に表示されますが、2番目のセルは最初のセルと同じプロパティを持つようです。しかし、いったん2番目のセルをタップしてから最初のセルをタップすると、2番目のセルが正しいセルデザインに切り替わります。2つのカスタムセルが正しく表示されないUITableView

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("EditCellID", forIndexPath: indexPath) as! EditCell 
    if indexPath.row == 0 
    { 
     //Do some stuff 
     return cell 
    } 
    if indexPath.row == 1 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("DeleteCellID", forIndexPath: indexPath) as! DeleteCell 
     //Do some stuff 
     return cell 
    } 

    return cell 
} 

答えて

0

これを試してみてください:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: UITableViewCell! 

    if indexPath.row == 0 { 
     cell = tableView.dequeueReusableCellWithIdentifier("EditCellID", forIndexPath: indexPath) as! EditCell 
     // Now you can access the subclass attributes by doing : (cell as! EditCell).theAttribute 
    } else { 
     cell = tableView.dequeueReusableCellWithIdentifier("DeleteCellID", forIndexPath: indexPath) as! DeleteCell 
    } 

    return cell 
} 

範囲に一つだけのセルオブジェクトを作成し、行に応じて、1つのあなたの特定のUITableViewCellのサブクラスのインスタンスとしてオブジェクトを初期化します。

UPDATE:UITableViewCellサブクラス属性へのアクセス方法に関するコメントを追加しました。

+0

意味がありますが、最初のifステートメントでセルのプロパティを設定しようとするとエラーが発生します。たとえば、EditCellには 'UITextField IBOutlet'がありますが、私が設定すると、 'Type of Value UITableViewCellにはメンバーがありませんtitleField'というエラーが出ます。何らかの理由で、セルが 'EditCell'に変更するのではなく、' UITableViewCell'タイプのままです。 – Brosef

+0

申し訳ありませんが、この質問を私の答えに追加するのを忘れました。ちょうどこれを行う: '(セルとして!EditCell).titleField' – Lucas

+0

うわー!それは完璧に機能しましたが、なぜ '(EditCellとしてのセルは).titleField'が必要ですか?この行の 'Cell = tableView.dequeueReusableCellWithIdentifier(" EditCellID "、forIndexPath:indexPath)でEditCellとしてセルをキャストしなかったかどうかを確認してください。 EditCell'? – Brosef

関連する問題