繁体   English   中英

Swift-如果检测到“滑动删除”,如何在自定义tableViewCell类中隐藏按钮?

[英]Swift - How to hide button in custom tableViewCell class if left “Swipe to delete” detected?

我有一个自定义单元格类,其中有2个标签显示一个单词及其翻译。 当用户点击其中一个标签时,我想打开一个仅显示标签内容的新视图控制器。 我们需要了解点击了哪个标签。

我无法使用tapGestureRecognizer和标签上的标签使其工作,因为标签是由类文件管理的。 我发现在标签顶部添加透明按钮更加容易。

对新视图控制器的设置工作正常。 但是我再也不能在tableViewCells上使用“刷卡删除”了。

当我们向左滑动时,该行向左移动,它在该行的右侧显示红色正方形的“ DELETE”按钮,但同时它对新的viewController执行segue。

如果检测到左侧的“滑动删除”,如何隐藏/停用tableViewCell按钮?

我试图添加一个左swipeGestureRecognizer并添加一个公共变量isSwipingLeft: Bool到不同的地方( viewDidEndEditing ... etc),但没有成功。 我所做的最好的事情是检测滑动,隐藏按钮,但是无法取消隐藏按钮...

编辑:

DetailListCell

protocol CustomCellDelegate {
func leftButtonTapped(cell: DetailListCell)
func rightButtonTapped(cell: DetailListCell)
}

class DetailListCell: UITableViewCell {

@IBOutlet weak var entryLeftLbl: UILabel!
@IBOutlet weak var entryRightLbl: UILabel!
@IBOutlet weak var leftButtonOutlet: UIButton!
@IBOutlet weak var rightButtonOutlet: UIButton!
var delegate: CustomCellDelegate?

func configureDetailListCell(entry: Entry) {
entryLeftLbl.isUserInteractionEnabled = true
entryRightLbl.isUserInteractionEnabled = true
// cell configuration to hide/display labels and buttons ...
}

@IBAction func leftButton(_ sender: Any) {
if isSwipingToDelete == false {
delegate?.leftButtonTapped(cell: self)
}}   

@IBAction func rightButton(_ sender: Any) {
if isSwipingToDelete == false {
delegate?.rightButtonTapped(cell: self)
}}}

DetailListVC

override func viewDidLoad() {
detailTableView.delegate = self
detailTableView.dataSource = self        

// Swipe left
let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(DetailListVC.swipeLeft(gestureRecognizer:)))
swipeLeft.direction = .left
self.view!.addGestureRecognizer(swipeLeft)
}

// Swipe left to detect Swipe to delete
func swipeLeft(gestureRecognizer: UISwipeGestureRecognizer) {
isSwipingToDelete = true
}

func leftButtonTapped(cell: DetailListCell) {
// Find the row of the textField
let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!        
// Find the Entry object of the row/textField selected
if let objs = controller.fetchedObjects , objs.count > 0 {
let item = objs[indexPath.row].faceA
performSegue(withIdentifier: "FullEntryVC", sender: item)
}}

func rightButtonTapped(cell: DetailListCell) {        
// Find the row of the textField
let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!        
// Find the Entry object of the row/textField selected
if let objs = controller.fetchedObjects , objs.count > 0 {
let item = objs[indexPath.row].faceB
performSegue(withIdentifier: "FullEntryVC", sender: item)
}}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FullEntryVC" {
if let destination = segue.destination as? FullEntryVC {
if let entryFace = sender as? String {
isEditMode = false
destination.entryLabelValue = entryFace
}}}}

如Valdmer所建议,我使用了UITableViewCell isEditing属性。 我在按钮的方法内添加了if语句,以检查if cell.isEditing == false 如果为false则运行performSegue(withIdentifier: ) ,如果为true则“ performSegue(withIdentifier: )删除”工作正常,并且该按钮不执行任何操作。

我删除了所有与NSNotificationswipeLeft(gestureRecognizer: )相关的代码。

这是按钮之一的更新代码:

func leftButtonTapped(cell: DetailListCell) {
    // Check if "swipe to delete" is detected:
    if cell.isEditing == false {

        // Find the row of the textField
        let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!

        // Find the Entry object of the row/textField selected
        if let objs = controller.fetchedObjects , objs.count > 0 {
            let item = objs[indexPath.row].faceA
            performSegue(withIdentifier: "FullEntryVC", sender: item)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM