2016-10-06 9 views
0

私のテーブルビューでは、レイアウトに応じて2つのセルクラスを切り替えることができます。だから、私はどの細胞クラスをdidEndDisplaying関数で選択するかを選ぶことができますか?didEndDisplayingで使用するセルクラスを選択してください

dequeueReusableCellのようにセルを選択する必要がありますか?

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if isBigCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellBig") as! BigTableViewCell 
     cell.myImageView.kf.cancelDownloadTask() 
    } else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellSmall") as! SmallTableViewCell 
     cell.myImageView.kf.cancelDownloadTask() 
    } 
} 





func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if isBigCell { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "CellBig") as! BigTableViewCell 

      let data = ads[(indexPath as NSIndexPath).row] 
      cell.configureWithData(data) 

      //Dont show highlight 
      cell.selectionStyle = UITableViewCellSelectionStyle.none 

      return cell 
     } else { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "CellSmall") as! SmallTableViewCell 

      let data = ads[(indexPath as NSIndexPath).row] 
      cell.configureWithData(data) 

      //Dont show highlight 
      cell.selectionStyle = UITableViewCellSelectionStyle.none 

      return cell 
     } 

    } 
+0

:UITableViewCellのが、ちょうどサブクラスとしてセルをキャストしてそこからキャンセルしてください。 – Bseaborn

+1

ビュー(セル)にダウンロードタスクを置くことは非常に悪い考えです。 View Controllerで 'NSOperation'(またはカスタムクラス)と辞書' [NSIndexPath:NSOperation]を使用してください。 – vadian

+0

@Bseaborn like:(cell as!BigTableViewCell).adImageView.kf.cancelDownloadTask()? – user2636197

答えて

3

セルはdidEndDisplayingメソッドで与えられます。 cellForRowAtメソッドと同じように、実際のタイプはindexPathに基づいて決定します。

didEndDisplayingの別のセルをデキューしないでください。提供されたcellは、indexPathに基づいてキャストしてください。

それはあなたがして、細胞の種類を決定するためにindexPathを使用していないようですので、その後、あなたのコードは次のようなものでなければなりません:あなたはすでにdidEndDisplayingセルであなたのセルへの参照を持っている

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if isBigCell { 
     if let bigcell = cell as? BigTableViewCell { 
      bigcell.myImageView.kf.cancelDownloadTask() 
     } 
    } else { 
     if let smallcell = cell as? SmallTableViewCell { 
      smallcell.myImageView.kf.cancelDownloadTask() 
     } 
    } 
} 
+0

Like:(cell as!BigTableViewCell).adImageView.kf.cancelDownloadTask()? – user2636197

+0

そういうものですが、 'indexPath'に基づいて' BigTableViewCell'か 'SmallTableViewCell'かどうかを判断する必要があります。 – rmaddy

+0

** indexPath **に基づいてどのようにチェックするのですか?もし私が私の質問で提供したコードを使用するとクラッシュしませんが、私は(BigTableViewCellとしてのセル).adImageView.kf.cancelDownloadTask()と(Cellとしての!SmallTableViewCell).adImageView.kf.cancelDownloadTask()セルをキャストできないので – user2636197

関連する問題