2

xCode 7 ベータ版と Swift を使用して、MGSwipeTableCells でテーブルビューを実装しています。各セルの左右両方にスワイプボタンが必要なため、これを行っています。これらのボタンは両方とも、テーブルビューからセルを削除する必要があります。

セルにボタンを追加するときに、便利なコールバック メソッドを使用してこれを実行しようとしました。

// Layout table view cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("newsFeedCell", forIndexPath: indexPath) as! NewsFeedCell
    cell.layoutIfNeeded()

    // Add a remove button to the cell
    let removeButton = MGSwipeButton(title: "Remove", backgroundColor: color.removeButtonColor, callback: {
        (sender: MGSwipeTableCell!) -> Bool in

        // FIXME: UPDATE model
        self.numberOfEvents--
        self.tableView.deleteSections(NSIndexSet(index: indexPath.section), withRowAnimation: UITableViewRowAnimation.Fade)
        return true
    })
    cell.leftButtons = [removeButton]

ただし、最初のセルを削除すると、すべてのインデックスが破棄され、コールバックが不適切なセルを削除するようになりました。つまり、cell_0 を削除すると、cell_1 がテーブルの最初の 0 番目になります。ただし、cell_1 に関連付けられたボタンのコールバックは、実際にはテーブルの 0 番目のセルであっても、インデックス 1 のセルを削除します。

MGSwipeTableCell デリゲート メソッドを実装しようとしましたが、役に立ちませんでした。これらのメソッドはどれも、コードの実行中に呼び出されたことはありません。この問題を解決するにはどうすればよいですか? デリゲートを実装すると、この問題は解決しますか? もしそうなら、例を提供することは可能でしょうか?そうでない場合は、セルを削除できるスワイプボタンを両側に備えたテーブルビューセルを使用する別の方法を提案できますか?

4

2 に答える 2

1

次のようにして、正しい indexPath を取得することもできます。

let removeButton = MGSwipeButton(title: "Remove", backgroundColor: color.removeButtonColor, callback: {
            (sender: MGSwipeTableCell!) -> Bool in

  let indexPath = self.tableView.indexPathForCell(sender)         
  self.tableView.deleteSections(NSIndexSet(index: indexPath.section), withRowAnimation: UITableViewRowAnimation.Fade)
            return true
        })
于 2016-04-06T04:33:37.717 に答える