在我的应用程序中,我有主屏幕,该屏幕在表格视图中显示电子邮件列表。单击任何电子邮件后,我们将重定向到电子邮件详细信息屏幕,我们可以在其中更新争执,已付款和未决等电子邮件的状态,并将状态从未读更改为已读电子邮件。
在详细信息屏幕上更改电子邮件的状态后,当我从详细信息视图弹出到电子邮件列表时,还希望在表视图中针对该特定电子邮件更新电子邮件列表。
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")
}
}
答案 0 :(得分:2)
详细信息当选择了一个表格视图单元格时,执行segue或按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")
}
}
}
使用选定的EmailDetailViewController
对象配置NewHomeModel
。根据需要在此视图控制器中更新对象的值。返回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
}
}
在MainViewController
的{{1}}闭包中,替换数据源数组中的更新对象,然后重新加载callBack
。
答案 1 :(得分:0)
您可以创建Delegate方法来重新加载数据。
protocol NewHomeDelegate: NSObjectProtocol {
func reloadData()
}
之后创建委托变量
weak var delegate: NewHomeDelegate?
并从牙齿视图控制器调用它。
调用该委托函数并刷新数据