[英]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
. 在
MainViewController
的callBack
闭包中,替换数据源数组中的更新对象,然后重新加载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.