我有一个自定义单元格类,其中有 2 个标签显示单词及其翻译。当用户点击其中一个标签时,我想打开一个新的 View Controller ,仅显示标签内容。我们需要了解哪个标签被点击。
我无法使用 tapGestureRecognizer
和标签上的标记使其工作,因为标签是由类文件管理的。我发现在标签顶部添加透明按钮更容易。
新 View Controller 的 segue 工作正常。但我无法再使用 tableViewCells
上的“滑动删除”功能。
当我们向左滑动时,该行向左移动,它会在该行的右侧显示红色方形的“DELETE”按钮,但同时它会执行到新 viewController 的 segue。
How can I hide / deactivate buttons in
tableViewCell
if left "Swipe to delete" is detected?
我尝试添加左侧 swipeGestureRecognizer
并将公共(public)变量 isSwipingLeft: Bool
添加到不同的位置(viewDidEndEditing
...等),而无需成功。我做的最好的就是检测滑动,隐藏按钮,但无法取消隐藏按钮......
编辑:
详细列表单元格
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)
}}}
详细列表VC
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
则“滑动删除”工作正常,按钮不执行任何操作。
我删除了与 NSNotification
和 swipeLeft(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)
}
}
}
关于ios - Swift - 如果检测到 "Swipe to delete",如何隐藏自定义 tableViewCell 类中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500977/