Arrays 在某些数据发生更改后,如何刷新TableView?
我只想更改一个名为Network的类中的用户列表。我不明白在用户列表改变后如何更新TableView。我将在下面的代码中向您展示一个示例和详细的问题Arrays 在某些数据发生更改后,如何刷新TableView?,arrays,swift,uitableview,Arrays,Swift,Uitableview,我只想更改一个名为Network的类中的用户列表。我不明白在用户列表改变后如何更新TableView。我将在下面的代码中向您展示一个示例和详细的问题 // Network.swift class Network { var userList: [User] = [] // Next functions may change userList array // For example, the data came from the server, and update t
// Network.swift
class Network {
var userList: [User] = []
// Next functions may change userList array
// For example, the data came from the server, and update the userList with new data
}
// App delegate
class AppDelegate: UIResponder, UIApplicationDelegate {
var network: Network = Network()
..
}
// File TableViewController.swift
class TableViewController: UITableViewController {
…
var userList: [User] = [] // Here I want to have a full copy of the array from Network class
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
self.userList = appDelegate.network.userList // Just copy an array
// And I want that after each update appDelegate.network.userList I updated the table, how to do it better?
self.tableView.reloadData()
}
}
您可以使用通知。每当用户列表更新时,发布如下通知:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UserlistUpdate"), object: nil)
然后,在viewDidLoad中添加:
NotificationCenter.default.addObserver(self, selector: #selector(TableViewController.reloadData), name: NSNotification.Name(rawValue: "UserlistUpdate"), object: nil)
另外,到目前为止,关于您的体系结构,我会让TableViewController为网络保存一个变量,而不是保存自己的用户数组。然后,在AppDelegate中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let network = Network()
// Access the TableViewController and set its network variable
let tableViewController = window!.rootViewController as! TableViewController
tableViewController.network = network
您可以使用通知。每当用户列表更新时,发布如下通知:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UserlistUpdate"), object: nil)
然后,在viewDidLoad中添加:
NotificationCenter.default.addObserver(self, selector: #selector(TableViewController.reloadData), name: NSNotification.Name(rawValue: "UserlistUpdate"), object: nil)
另外,到目前为止,关于您的体系结构,我会让TableViewController为网络保存一个变量,而不是保存自己的用户数组。然后,在AppDelegate中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let network = Network()
// Access the TableViewController and set its network variable
let tableViewController = window!.rootViewController as! TableViewController
tableViewController.network = network
正如评论中提到的@JDM,您的体系结构很混乱。
尝试使用协议执行此委派:
// Network.swift
protocol UsersUpdatedProtocol {
func usersListUpdated(list: [User])
}
class Network {
var userList: [User] = [] {
didSet {
delegate?.usersListUpdated(list: userList)
}
}
var delegate: UsersUpdatedProtocol?
init(delegate d: UsersUpdatedProtocol) {
super.init()
delegate = d
}
}
// File TableViewController.swift
class TableViewController: UITableViewController, UsersUpdatedProtocol {
var userList: [User] = []
override func viewDidLoad() {
super.viewDidLoad()
let _ = Network(delegate: self)
}
func usersListUpdated(list: [User]) {
self.userList = list
self.tableView.reloadData()
}
}
正如评论中提到的@JDM,您的体系结构很混乱。
尝试使用协议执行此委派:
// Network.swift
protocol UsersUpdatedProtocol {
func usersListUpdated(list: [User])
}
class Network {
var userList: [User] = [] {
didSet {
delegate?.usersListUpdated(list: userList)
}
}
var delegate: UsersUpdatedProtocol?
init(delegate d: UsersUpdatedProtocol) {
super.init()
delegate = d
}
}
// File TableViewController.swift
class TableViewController: UITableViewController, UsersUpdatedProtocol {
var userList: [User] = []
override func viewDidLoad() {
super.viewDidLoad()
let _ = Network(delegate: self)
}
func usersListUpdated(list: [User]) {
self.userList = list
self.tableView.reloadData()
}
}
您的体系结构非常无序和糟糕(为什么要在appdelegate中为此保留一个变量),这个列表是如何更新的?谁是网络的调用者来检查更新?您的体系结构非常无序和糟糕(为什么要在appdelegate中为此保留一个var)此列表是如何更新的?谁是网络的调用者来检查更新?如果用户列表中的更改可以有多个响应,那么这种方法会很好,因为他们每个人都可以订阅通知。至于体系结构,我可以建议一种单例方法:public class
MyClass{static let sharedistance=MyClass()private init(){}
如果用户列表中的更改可以有多个响应,那么这个方法会很好,因为每个用户都可以订阅通知。至于体系结构,我可以建议一种单例方法:public classMyClass{static let sharedInstance=MyClass()private init(){}
如果只有一件事要响应用户列表的更新,则此解决方案有效。如果有多件事要响应,则最好使用通知。如果只有一件事要响应用户列表的更新,则此解决方案有效。如果有多件事要响应,则您可能会更好使用通知