swift - trailingSwipeActions 和旋转问题

标签 swift uitableview swipe ios11

我尝试实现新方法 trailingSwipeActionsForRowAtleadingSwipeActionsConfigurationForRowAt 来显示带有图标的行操作...

不幸的是,当我在拖尾滑动后将设备从横向旋转到纵向时,我遇到了一个奇怪的错误。

这是我的代码

var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView = UITableView()
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    tableView.estimatedRowHeight = 100

    view.addSubview(tableView)

    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 15
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
    cell.textLabel?.text = "\(indexPath.row)"
    cell.detailTextLabel?.text = "\(indexPath.row)"
    cell.backgroundColor = .lightGray

    return cell
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let deleteAction = UIContextualAction(style: .normal, title: "") { (action, view, handler) in
        print("Delete Action Tapped")

    }
    deleteAction.backgroundColor = .red
    deleteAction.image = UIImage(named: "icons8-info-50")


    let defaultAction = UIContextualAction(style: .normal, title: nil) { (action, view, handler) in
        print("default Action Tapped")
    }

    defaultAction.backgroundColor = .orange
    defaultAction.image = UIImage(named: "icons8-info-50")

    let swipeAction = UISwipeActionsConfiguration(actions: [deleteAction, defaultAction])
    swipeAction.performsFirstActionWithFullSwipe = false
    return swipeAction
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let deleteAction = UIContextualAction(style: .normal, title: "") { (action, view, handler) in
        print("Delete Action Tapped")
    }
    deleteAction.backgroundColor = .blue
    deleteAction.image = UIImage(named: "icons8-info-50")

    let swipeAction = UISwipeActionsConfiguration(actions: [deleteAction])
    swipeAction.performsFirstActionWithFullSwipe = false
    return swipeAction
}

横向模式: enter image description here

然后切换到纵向模式: enter image description here

有什么想法吗?

谢谢

最佳答案

参见 this question .

您可以重置单元格的编辑状态,以便在 viewWillTransition(to:with:) 中使用 setEditing(_:animated:) 不再显示操作,当设备旋转时调用。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    tableView.setEditing(false, animated: true)
}

这仅在您调用 setEditing(_:animated:) 外部 协调器的 animate 方法时有效。在 animation block 中将 editing 设置为 false 将无效。

关于swift - trailingSwipeActions 和旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48153346/

相关文章:

iphone - 为什么 UITableView 的滑动删除有时工作正常有时不工作?

ios - Tesseract OCR w/iOS & Swift 返回错误或乱码

ios - 如何使 searchResultsTableView 使用自定义单元格而不是默认单元格

ios - 如何让CollectionView和TableView同时滚动?

ios - 如何在 WkWebView 中检测向后滑动

java - 向左/向右滑动事件

swift - 21如何计算! (21 阶乘)快速?

ios - 如何使用 NSLayoutConstraint 以编程方式使容器中的表页脚 View 居中?

ios - 导航时关闭 View Controller

ios - UITableViewController 中的 UIActivityIndi​​catorView