xcode - 在 TableViewCell 中滑动后删除 TableViewController 中的行

标签 xcode swift uitableview core-data uipangesturerecognizer

我正在使用 CoreData 使用 UITableViewController.swift 和 UITableViewCell.swift 构建一个应用程序。

我正在尝试通过在 UITableViewCell.swift 中使用 UIPanGestureRecognizer 删除一行,就像在 Clear to-do 应用程序中一样。我能够向左和向右平移行,但我不确定如何获取这些选定行的 indexPath 并在所有数据所在的 UITableViewController.swift 中删除它们。

EduDicTableViewCell.swift:

override func awakeFromNib() {
    super.awakeFromNib()

    let recognizer = UIPanGestureRecognizer(target: self, action: #selector(EduDicTableViewCell.handlePan(_:)))

    recognizer.delegate = self
    addGestureRecognizer(recognizer)
}

//MARK: - horizontal pan gesture methods
func handlePan(recognizer: UIPanGestureRecognizer) {
    // 1
    if recognizer.state == .Began {
        // when the gesture begins, record the current center location
        originalCenter = center
    }
    // 2
    if recognizer.state == .Changed {
        let translation = recognizer.translationInView(self)
        center = CGPointMake(originalCenter.x + translation.x, originalCenter.y)
        // has the user dragged the item far enough to initiate a delete/complete?
        deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
    }
    // 3
    if recognizer.state == .Ended {
        let originalFrame = CGRect(x: 0, y: frame.origin.y,
                                   width: bounds.size.width, height: bounds.size.height)
        if deleteOnDragRelease {
            print("send it")



        } else {
            UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
            print("Bounced Back")
        }
    }
}

感谢阅读!

最佳答案

您可以在自定义单元格中使用委托(delegate)/协议(protocol),它会在 TableView Controller 中调用合适的方法:

向单元格添加协议(protocol)定义:

protocol EduDicTableViewCellDelegate {
    func didSwipeDelete(cell: UITableViewCell)
}

然后使用此协议(protocol)添加一个(可选的)delegate 变量:

var delegate : EduDicTableViewCellDelegate? = nil

handlePan方法中,添加一行在松开pan时调用delegate方法:

if deleteOnDragRelease {
    print("send it")
    self.delegate?.didSwipeDelete(self)
} else ...

请注意,didSwipeDelete 方法传递 self - 被滑动的单元格。

在tableviewcontroller中添加删除cell的方法(使用tableView的indexPathForCell方法获取被滑动的cell对应的indexPath):

func didSwipeDelete(cell: UITableViewCell) {
    if let indexPath = self.tableView.indexPathForCell(cell) {
        print("didSwipeDelete \(indexPath.section) - \(indexPath.row)")
        // remove the object at this indexPath from the data source
        // and delete the corresponding table view row
        ...
    }
}

修改 TableView Controller 的类定义以表明它采用协议(protocol):

class CustomTableViewController: UITableViewController, EduDicTableViewCellDelegate {
    ...
}

最后,在 cellForRowAtIndexPath 方法中,将单元格上的 delegate 变量设置为 self( TableView Controller ):

cell.delegate = self

关于xcode - 在 TableViewCell 中滑动后删除 TableViewController 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467891/

相关文章:

xcode - 从运行脚本阶段获取当前方案名称

ios - 运行 MPMoviePlayerController 时崩溃

ios - 重新排序/移动 UITableViewCell 但移回

ios - 在自动布局中使用多行 UIButtons 设置 UITableViewCell 高度

Swift Firebase 将数据检索到 TableView 中

iphone - 在单击取消按钮之前,搜索栏不会显示结果

objective-c - 获取 USB 硬盘的序列号 (Mac OS)

ios - UICollectionViewCell 导航

ios - navigationBar.isHidden 和 setNavigationBarHidden 的区别

swift - 将 Whatsapp 消息发送到特定的联系电话(Swift 项目)