简体   繁体   English

如何在Swift中从Detail ViewController更新tableview数组

[英]How to update array of tableview from Detail ViewController in Swift

In my application I have home screen which shows email listing in tableview. 在我的应用程序中,我有主屏幕,该屏幕在表格视图中显示电子邮件列表。 After click on any email we redirected to email detail screen where we can update status of email like dispute , paid and pending and also change the status from unread to read email. 单击任何电子邮件后,我们将重定向到电子邮件详细信息屏幕,在该屏幕上我们可以更新电子邮件状态(例如,争议,已付款和未决),还可以将状态从未读更改为已读电子邮件。

After changing status of email on detail screen it is also want to update on email listing in tableview for that specific email when i pop from detail view to email listing. 在详细信息屏幕上更改电子邮件的状态后,当我从详细信息视图弹出到电子邮件列表时,还希望在表视图中针对该特定电子邮件更新电子邮件列表。

struct NewHomeModel {
var body: String?
var date : String?
var dispute: Int?
var fileStatus: Int?
var from: String?
var msg_id: String?
var paid: Int?
var pending: Int?
var subject: String?
var thread_id: String?
var unread : Int?
var nextToken : String?

init(jsonData: [String: Any]) {
    body = jsonData["body"] as? String ?? ""
    date = jsonData["date"] as? String ?? ""
    dispute = jsonData["dispute"] as? Int ?? 0
    fileStatus = jsonData["fileStatus"] as? Int ?? 0
    from = jsonData["from"] as? String ?? ""
    msg_id = jsonData["msg_id"] as? String ?? ""
    paid = jsonData["paid"] as? Int ?? 0
    pending = jsonData["pending"] as? Int ?? 0
    subject = jsonData["subject"] as? String ?? ""
    thread_id = jsonData["thread_id"] as? String ?? ""
    unread = jsonData["unread"] as? Int ?? 0
}}
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if arrayData.count > 0 && arrayData.count > indexPath.row {
        let objemail = arrayData.object(at: indexPath.row) as? NewHomeModel

        let emailDetailVC = EmailDetailViewController()
        emailDetailVC.strThreadId = (objemail?.thread_id)!
        emailDetailVC.strTitle = (objemail?.subject)!
        emailDetailVC.homeModel = objemail
        self.navigationController?.pushViewController(emailDetailVC, animated: true)
    }else{
        print("array empty")
    }
}

detailvieWhen a table view cell is selected perform segue or push EmailDetailViewController . detailvie当选择了表格视图单元格时,执行segue或推送EmailDetailViewController Pass the selected NewHomeModel object to the EmailDetailViewController . 将所选的NewHomeModel对象传递给EmailDetailViewController

class MainViewController: UITableViewController {
    var arrayData:[NewHomeModel] = [NewHomeModel]()
    //...
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if arrayData.count > 0 && arrayData.count > indexPath.row {
            let objemail = arrayData[indexPath.row]

            let emailDetailVC = EmailDetailViewController()
            emailDetailVC.strThreadId = (objemail.thread_id)!
            emailDetailVC.strTitle = (objemail.subject)!
            emailDetailVC.homeModel = objemail
            emailDetailVC.callBack = { [weak self] objemail in
                self?.arrayData[indexPath.row] = objemail
                self?.tableView.reloadRows(at: [indexPath], with: .automatic)
            }
            self.navigationController?.pushViewController(emailDetailVC, animated: true)
        }else{
            print("array empty")
        }
    }
}

Configure the EmailDetailViewController with the selected NewHomeModel object. 配置EmailDetailViewController与所选择的NewHomeModel对象。 Update the values of the object as per your need in this view controller. 根据需要在此视图控制器中更新对象的值。 When going back to MainViewController pass the updated object to the previous view controller using callBack closure. 返回MainViewController ,使用callBack闭包将更新后的对象传递给先前的视图控制器。

class EmailDetailViewController: UIViewController {
    var strThreadId:String?
    var strTitle :String?
    var homeModel: NewHomeModel?
    var callBack: ((NewHomeModel)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        homeModel?.unread = 0
        //configure views with selectedModel
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if self.isMovingFromParent, let homeModel = homeModel {
            print("Going back to main view controller")
            callBack?(homeModel)
        }
    }
    func updateStatus() {
        homeModel?.dispute = 1
        homeModel?.paid = 1
        homeModel?.pending = 0
        homeModel?.unread = 0
    }
}

In MainViewController 's callBack closure replace the updated object in the data source array and reload the tableview . MainViewControllercallBack闭包中,替换数据源数组中的更新对象,然后重新加载tableview

You can create Delegate Method to reload data. 您可以创建Delegate方法来重新加载数据。

protocol NewHomeDelegate: NSObjectProtocol {
func reloadData()
}

after that create delegate variable 之后创建委托变量

weak var delegate: NewHomeDelegate? 

and call it from dental view controller. 并从牙科视图控制器调用它。

Call that delegate function and refresh data 调用该委托函数并刷新数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Swift 中的按钮从数组更新 tableView 中的单元格? - How to update cell in a tableView from array by using button in Swift? 如何在Swift中从其他ViewController重新加载TableView中的数据 - How to reload data in a TableView from a different ViewController in Swift 如何从 viewcontroller1 (Swift) 在 vi​​ewcontroller2 中附加列表和重新加载 tableView - How to append list and reload tableView in viewcontroller2 from viewcontroller1 (Swift) Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller 在Swift中将图像从tableView传递到viewController - Pass image from tableView to viewController in Swift Swift - 从另一个 ViewController 在 TableView 中插入项目 - Swift - insert item in TableView from another ViewController Swift 4:将数据从Tableview传递到ViewController - Swift 4: Passing Data from Tableview to ViewController 从另一个ViewController Swift 4.2重新加载tableView - reload tableView from Another ViewController Swift 4.2 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 从明细视图控制器返回后,在嵌入式TableView单元格中停止activityIndi​​cator - Stop activityIndicator in Embedded TableView Cell Upon Return From Detail ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM