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

时间:2017-04-19 16:13:44

标签: ios swift uitableview uigesturerecognizer custom-cell

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

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

新视图控制器的segue工作正常。但我不能再使用tableViewCells上的“轻扫删除”。

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

  

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

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

修改

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
}}}}

1 个答案:

答案 0 :(得分:0)

正如Valdmer建议我使用isEditing的{​​{1}}属性。我在我的按钮的方法中添加了UITableViewCell语句来检查if。如果if cell.isEditing == false然后false运行,如果performSegue(withIdentifier: ),那么"滑动删除"工作正常,按钮什么都不做。

我删除了相对于trueNSNotification的所有代码。

以下是其中一个按钮的更新代码:

swipeLeft(gestureRecognizer: )