1

こんにちは。
調べてもどうしても出てこなくて困っています。
UITableViewの並び替えた時のセルの更新について質問させていただきます。


func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    //一時的に保存
    let cellData = cellDatas[sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellDatas.insert(cellData, atIndex: destinationIndexPath.row)   
}

と、このようにデータソースを入れ替えるようにすると思うのですが
この後にどうしてもtableViewのセルの更新を行いたくて、

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    //一時的に保存
    let cellData = cellDatas[sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellDatas.insert(cellData, atIndex: destinationIndexPath.row)
    //リロードを追加
    tableView.reloadData()  
}

このようにするとセルのデータは確かに更新されるのですが、
≡ をつかんで離した時の挙動が早すぎて不自然です。そこで

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    //一時的に保存
    let cellData = cellDatas[sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellDatas.insert(cellData, atIndex: destinationIndexPath.row)
    //リロード開始
    tableView.beginUpdates()
    tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)
    tableView.endUpdates()
    //リロード終了
}

と書いてみたのですが今度は正常にセルがリロードされません。


セルをリロードしたい理由につきましては、セルの持つ部品のプロパティに現在のCellの行番号を保持させて置きたいのです。(以下を ≡ を動かした後もう1度呼びたい)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //この中でcellの持つ部品のプロパティに値を設定する
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath:indexPath) as! CustomCell
    cell.view.row = indexPath.row
}



おかげさまで解決しました。ありがとうございます。
この質問の下にコメントを残したのですが、マークダウンに慣れていなくて、見にくくなってしまいました。
コードだけでも書き直したいと思います。申し訳ございません。

CustomCellのプロパティにクロージャをもたせておいて

var buttonTapAction = {}

セルを返却するメソッドでViewController内のメソッドを渡して

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath:indexPath) as! CustomCell 
    cell.buttonTapAction = {self.buttonTapAction(cell)} 
} 

同じViewController内のメソッド(ここではbuttonTap‌​Action(_:))を用意

func buttonTapAction(cell:TextTableViewCell) { 
    let row = tableView.indexPathForCell(cell)?.row 
    //rowを使った処理
}

と別の部分で取得することができました。

1
  • cellindexPath の情報を持たせておかなくても、必要なタイミングで indexPath(for:) を呼び出すことで、cell から indexPath を取得することができます。
    – quesera2
    2016年10月22日 2:29

1 件の回答 1

-1

質問者さんの意図を完全に理解した自信はないのですが、リロードを遅らせたいのでしたら、GCD(Grand Central Dispath)等を利用して、遅延処理することになるでしょう。

Swift 3

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let cellData = cellDatas[sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellDatas.insert(cellData, atIndex: destinationIndexPath.row)
    // 1秒遅らせてリロード
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(1), execute: {tableView.reloadData()})  
}

Swift 2

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let cellData = cellDatas[sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellDatas.insert(cellData, atIndex: destinationIndexPath.row)
    // 1秒遅らせてリロード
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, (NSEC_PER_SEC * 1))
    dispatch_after(delayTime, dispatchQueue, {tableView.reloadData()})
}
1
  • コメント、回答いただいた方々、本当にありがとうございます。 Swift3 も合わせてコメント助かりました。 //データを返すメソッド func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath:indexPath) as! CustomCell cell.buttonTapAction = {self.buttonTapAction(cell)} } とデータを返すメソッド内でcellを引数に渡して同じViewController内のメソッド(ここではbuttonTapAction(_:))を用意し、そこで func buttonTapAction(cell:TextTableViewCell) { let row = tableView.indexPathForCell(cell)?.row } と取得することができました。 2016年10月22日 13:25

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。