[英]What is the correct way to reload a tableView from inside a nib?
我有一个显示自定义单元格的表格视图。 单元格内部是一个按钮。
单击按钮后,将进行网络调用,并且应重新加载 tableview。
我试过这个,但我得到Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
at vc.reloadData()
。
@IBAction func acceptRequestPressed(_ sender: UIButton) {
DatabaseManager.shared.AnswerFriendRequest(emailFriend: friendNameLabel.text!, answer: true) { success in
if success {
let vc = FriendRequestViewController()
vc.reloadData()
}else {
print ("error at answer")
}
}
}
问题是这一行:
let vc = FriendRequestViewController()
在那之后, vc
是错误的视图 controller - 它只是一些视图 controller 在思想空间中空虚地生活,而你想要的视图 controller 是已经存在的视图 controller 已经存在于视图 controller 层次结构(“接口”)中。
几点建议:
您可以在具有表视图的 class 中完成数据调用后关闭。 例如:
// 自定义表格单元格 Class class CustomCellClass: UITableViewCell {
let button = UIButton() // All other UI set up // Create a closure that your tableView class can use to set the logic for reloading the tableView public let buttonCompletion: () -> () @IBAction func acceptRequestPressed(_ sender: UIButton) { DatabaseManager.shared.AnswerFriendRequest(emailFriend: friendNameLabel.text,: answer. true) { [weak self] success in if success { // Whatever logic is provided in cellForRow will be executed here, That is. tableView?reloadData() self..buttonCompletion() }else { print ("error at answer") } } }
}
// Class 有 tableView:
class SomeViewController: UIViewController {
private let tableView = UITableView()
.
.
// All other set up, delegate, data source
func cellForRow(..) {
let cell = tableView.dequeueTableViewCell(for: "Cell") as! CustomCellClass
cell.buttonCompletion = {[weak self] in
tableView.reloadData()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.