微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在使用自定义 TableViewCell 按钮执行 segue 之前更新数据?

如何解决如何在使用自定义 TableViewCell 按钮执行 segue 之前更新数据?

目前,我有一个屏幕显示带有自定义单元格的 TableView,每个单元格包含一个 timeButton。当您按下 timeButton 时,它会转到弹出屏幕以选择时间。当它被关闭时,它会在主屏幕上更新 timeButton 的标题

我有一个委托方法,每次按下时都会使用按下的 timeButton 的 indexPath.row 更新 rowIndex(实例变量)。我注意到 segue 方法rowIndex 被 presstimeButton(cell: TaskCell) 委托方法更新之前运行。如何让委托方法在 segue 发生之前更新 rowIndex

这里是主要的VC的代码

struct Task {
    var name,time: String
}

class MainViewController: UIViewController {

    @IBOutlet weak var taskList: UITableView!
    
    var tasks = [Task]()
    var rowIndex = Int()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set initial task time's value
        tasks.append(Task(name: "",time: "Set time"))
        
        taskList.delegate = self
        taskList.dataSource = self
    }

    // Segue to pop-up screen
    override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
        if segue.identifier == "seguetoPopUp" {
            let controller = segue.destination as! PopUpViewController
            
            // ISSUE: rowIndex isn't updated when segue starts
            if tasks[rowIndex].time != "Set time" {    
                // if time is already set,reset time to "0:00"
                tasks[rowIndex].time = "0:00"
            } else {
                // if time is not set 
            } 
    }
    
    // Unwind from pop-up screen
    @IBAction func unwindFromPopUp(_ segue: UIStoryboardSegue) {
        // code to update "timeButton" with selected time
        tasks[rowIndex].time = controller.selectedTasktime
        ...
    }
}

extension TaskListViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return tasks.count
    }
    
    // Return custom cell + data to show in table view
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell",for: indexPath) as! TaskCell
        cell.delegate = self

        // Configure timeButton in taskCell
        let task = tasks[indexPath.row]
        cell.timeButton.setTitle(task.time,for: .normal)

        return cell
    }
}

extension TaskListViewController: TaskCellDelegate {
    // ISSUE: rowIndex updates after segue method instead of before 
    func pressedTimeButton(onCell cell: TaskCell) {
        if let indexPath = taskList.indexPath(for: cell) {
            rowIndex = indexPath.row        
        }
    }
}

这是我的自定义单元格的代码 + 其委托协议:

protocol TaskCellDelegate: class {
    func pressedTimeButton(onCell cell: TaskCell)
}

class TaskCell: UITableViewCell,UITextFieldDelegate {
    weak var delegate: TaskCellDelegate?
    
    @IBOutlet weak var timeButton: UIButton!

    override func prepareForReuse() {
        super.prepareForReuse()
        delegate = nil
    }
    
    @IBAction func tapTimeButton(_ sender: UIButton) {
        delegate?.pressedTimeButton(onCell: self)
    }
}

解决方法

似乎您已在故事板中将 tableView cell/timeButton 的 segue 设置为 ANativeWindow_setBuffersGeometry(new_native_window,500,WINDOW_FORMAT_RGBA_8888); ,它的作用是在点击 cell/timeButton 时立即触发 segue,而不是从 PopUpViewController 拖动 segue到 ViewController 这样只有在您调用 PopUpViewController

时才会执行 segue

enter image description here

还实现self.performSegue(withIdentifier: "segueToPopUp",sender: nil)(如果您希望在单元格被点击时执行转场)此方法将在单元格被点击时触发,执行您的任务时间更新。

func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath)

如果您不想在点击单元格时进行转场,而只需要按时间按钮进行转场,那么您现有的 func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) { rowIndex = indexPath.row self.performSegue(withIdentifier: "segueToPopUp",sender: nil) } 方法现在应该可以正常工作

pressedTimeButton

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。