[英]Swift - How to hide button in custom tableViewCell class if left “Swipe to delete” detected?
I have a custom cell class with 2 labels displaying a word and its translation. 我有一个自定义单元格类,其中有2个标签显示一个单词及其翻译。 When the user taps one of the labels, I want to open a new view controller with only the label content displayed. 当用户点击其中一个标签时,我想打开一个仅显示标签内容的新视图控制器。 We need to understand which label was tapped. 我们需要了解点击了哪个标签。
I could not managed to make it work using tapGestureRecognizer
and tag on labels because the labels were managed by the class file. 我无法使用tapGestureRecognizer
和标签上的标签使其工作,因为标签是由类文件管理的。 I found it easier to add transparent buttons on top of labels. 我发现在标签顶部添加透明按钮更加容易。
The segue to the new view controller works fine. 对新视图控制器的设置工作正常。 But I cannot use anymore the "swipe to delete" on tableViewCells
. 但是我再也不能在tableViewCells
上使用“刷卡删除”了。
When we do the left swipe, the row is moving left, it displays the red squared "DELETE" button on the right side of the row, but at the same time it performs the segue to the new viewController. 当我们向左滑动时,该行向左移动,它在该行的右侧显示红色正方形的“ DELETE”按钮,但同时它对新的viewController执行segue。
How can I hide / deactivate buttons in
tableViewCell
if left "Swipe to delete" is detected? 如果检测到左侧的“滑动删除”,如何隐藏/停用tableViewCell
按钮?
I tried to add a left swipeGestureRecognizer
and add a common variable isSwipingLeft: Bool
to different places ( viewDidEndEditing
...etc) without success. 我试图添加一个左swipeGestureRecognizer
并添加一个公共变量isSwipingLeft: Bool
到不同的地方( viewDidEndEditing
... etc),但没有成功。 The best I did was to detect the swipe, hide the button, but could not unhide the button back... 我所做的最好的事情是检测滑动,隐藏按钮,但是无法取消隐藏按钮...
EDIT: 编辑:
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)
}}}
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
}}}}
As Valdmer proposed I used isEditing
property of UITableViewCell
. 如Valdmer所建议,我使用了UITableViewCell
isEditing
属性。 I added a if
statement inside the methods of my buttons to check if cell.isEditing == false
. 我在按钮的方法内添加了if
语句,以检查if cell.isEditing == false
。 If false
then performSegue(withIdentifier: )
is run, if true
then the "swipe to delete" is working fine and the button does nothing. 如果为false
则运行performSegue(withIdentifier: )
,如果为true
则“ performSegue(withIdentifier: )
删除”工作正常,并且该按钮不执行任何操作。
I removed all piece of code relative to NSNotification
and to swipeLeft(gestureRecognizer: )
. 我删除了所有与NSNotification
和swipeLeft(gestureRecognizer: )
相关的代码。
Here is the updated code for one of the button: 这是按钮之一的更新代码:
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.