
[英]Swift tableView.reloadData() does not reload table view in Swift
[英]Why does not reload tableView work swift?
当我从delegate
(或viewDidLoad
)调用reloadData()
,函数tableView(...cellForRowAtIndexPath:..)
不会被触发。 当我从 tapButton 调用reloadData()
,一切正常。 为什么?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
exerciseList = ExerciseListTableViewController.ExerciseList
trainingList = TrainingListTableViewController.trainingList
if UserDefaults.standard.integer(forKey: "FactoryEx") != 0 {
identifierFactory = UserDefaults.standard.integer(forKey: "FactoryEx")
}
tableView.rowHeight = 60
tableView.allowsSelection = false
//tableView.tableFooterView = UIView()
}
func reload(choose: Bool){
exerciseTableChoose = choose
tableView.reloadData()
}
@IBAction func reload(_ sender: UIButton) {
exerciseList.append(Exercise())
tableView.reloadData()
}
protocol FirstTable {
func reload(choose: Bool)
func addCell(name: String, parametr: [Bool])
}
在viewDidAppear() 中添加tableView.reloadData () 。 因为viewDidLoad()只在 Viewcontroller 创建时调用。
这样做。
extension SCSelectSubTypeVC : UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:SCSelectSubTypeCell = self.tblView.dequeueReusableCell(withIdentifier: "SCSelectSubTypeCell") as! SCSelectSubTypeCell
cell.confirgureCell(datasource[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
}
并在 viewWillAppear() 函数中调用 self.tblView.reloadData() 如果您想在每次 viewController 出现时重新加载表。
不要忘记,UI只能在主线程中更新!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.