在我的应用程序中,我已滑动以在分段控件中删除,并且它通过显示两个不同的表视图来工作,但是我只希望滑动以删除并报告以在我的情况0中显示,而不是在我的情况1中显示(因为情况1)包含其他用户当前用户无法删除或报告的数据)。

我尝试使用:

if mySegmentedControl.selectedSegmentIndex == 0 {
        return UISwipeActionsConfiguration(actions: [delete, report])
    }
    else {
        return nil
    }


但这仅删除了报告按钮,而不删除了删除按钮?为什么是这样?

这是我的删除和报告代码的滑动:

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

let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, view, nil) in
    Database.database().reference().child("messages").child(self.currentUserID!).child(self.message[indexPath.row].messageID).setValue([])
}

let report = UIContextualAction(style: .destructive, title: "Report") { (action, view, nil) in

    let areUSureAlert = UIAlertController(title: "Are you sure you want to report?", message: "This will block this user from message you.", preferredStyle: UIAlertControllerStyle.alert)
    areUSureAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in

        Database.database().reference().child("messages").child(self.currentUserID!).child(self.message[indexPath.row].messageID).setValue([])

        let blockedPost = ["timestamp" : [".sv" : "timestamp"]]
        Database.database().reference().child("blocked").child(self.currentUserID!).child(self.message[indexPath.row].fromID).setValue(blockedPost)
    }))

    areUSureAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
        //dismiss(animated: true, completion: nil)
    }))

    self.present(areUSureAlert, animated: true, completion: nil)
 }
report.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.8918681279, blue: 0, alpha: 1)

if mySegmentedControl.selectedSegmentIndex == 0 {
        return UISwipeActionsConfiguration(actions: [delete, report])
    }
    else {
        return nil
    }

}

最佳答案

在此处找到解决方案:Delete button is automatically implemented on swipe despite not implementing trailing swipe action in tableview

通过实施:

return UISwipeActionsConfiguration.init()


代替

return nil

关于swift - 分段控件-为什么选中.selectedSegmentIndex时两个滑动 Action 都不会消失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52813205/

10-12 00:32
查看更多