2016-08-10 8 views
0

カスタム表ビューのセルを実装し、dequeueReusableCellWithIdentifierで以下のエラーが表示される場合は、いずれかお手伝いください。'dequeueReusableCellWithIdentifier'の候補は、予想されるコンテキスト結果のタイプ 'DynamicTableViewCell?'を生成しません。

コード:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

     //static DynamicTableViewCell *cell = nil; 
     var cell : DynamicTableViewCell? 

     var token: dispatch_once_t = 0 
     dispatch_once(&token) {() -> Void in 
      cell = tableView.dequeueReusableCellWithIdentifier("cell") 
     } 

     self.setUpCell(cell!, indexPath: indexPath) 

     return self.calculateHeightForConfiguredSizingCell(cell!) 

    } 
+0

***静的なDynamic ** TableViewCell *は矛盾しています。このコードはまったく動作しません。 'cellForRowAtIndexPath'の外側の' dequeueReusableCellWithIdentifier'は無意味です。 – vadian

+0

私はその行をコメントしていると私はそれを使用していない、私は参照の作品の同じ目的のCバージョンがあります: - https://github.com/tjosten/iOS-UITableViewCell-with-dynamic-height/私はしようとしています迅速に書く – Max

+0

tableViewのcellForRowAtIndexPathメソッドでのみdequeueReusableCellWithIdentifierを使用してください。カスタムセルの場合は、コントローラをロードしている間にカスタムクラスを登録する必要があります(たとえばviewDidLoadなど) – KostiaZzz

答えて

1

私はそれを行うためのあなたの方法は、安定した十分なソリューションであるかどうかわからないんだけど、あなたが指摘Objective-Cのコードのようにスウィフトに翻訳することができます。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    struct My { 
     static var cell : DynamicTableViewCell? 
     static var token: dispatch_once_t = 0 
    } 

    dispatch_once(&My.token) {() -> Void in 
     My.cell = tableView.dequeueReusableCellWithIdentifier("cell") as? DynamicTableViewCell 
    } 

    self.setUpCell(My.cell!, indexPath: indexPath) 

    return self.calculateHeightForConfiguredSizingCell(My.cell!) 
} 

エラーメッセージ「dequeueReusableCellWithIdentifier」の候補では、予想されるコンテキスト結果のタイプ「DynamicTableViewCell?」が生成されません。は、戻りタイプがdequeueReusableCellWithIdentifierであり、タイプDynamicTableViewCell?の変数に割り当てることができないUITableViewCell?であると言っています。このエラーを抑止するには、明示的なキャストが必要です。

しかし、それ以外のローカルcelltokenを静的にする必要があります。

また、Swift 3は標準ライブラリからdispatch_onceを除外していることも知っておく必要があります。

関連する問題