gpt4 book ai didi

ios - Swift MGSwipeTableCell 滑动删除单元格

转载 作者:可可西里 更新时间:2023-11-01 02:19:38 25 4
gpt4 key购买 nike

我正在使用 xCode 7 beta 和 Swift 来实现带有 MGSwipeTableCells 的表格 View 。我这样做是因为我需要在每个单元格的左侧和右侧都有一个滑动按钮。这两个按钮都需要从表格 View 中删除单元格。

我尝试通过在将按钮添加到单元格时使用方便的回调方法来执行此操作:

// 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 关联的按钮的回调会删除索引为 1 的单元格,即使它现在实际上是表中的第 0 个单元格。

我试图实现 MGSwipeTableCell 委托(delegate)方法,但无济于事。在我的代码执行过程中,这些方法都没有被调用过。我应该如何解决这个问题?实现委托(delegate)会解决这个问题吗?如果可以,能否举个例子?如果没有,您能否建议一种替代方法,让 tableview 单元格的两侧都带有可以删除所述单元格的滑动按钮?

最佳答案

你也可以做这样的事情来获得正确的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
})

关于ios - Swift MGSwipeTableCell 滑动删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com