【问题标题】:Why does not reload tableView work swift?为什么重新加载 tableView 不能快速工作?
【发布时间】:2020-03-09 11:09:53
【问题描述】:

当我从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])
}

【问题讨论】:

  • 可能有多种原因,计数为零,部分中的项目数返回 0
  • 你实现了'numberOfRowsForSection'数据源方法吗?
  • 添加与UITableView 创建相关的代码以及为TableView 设置数据的位置并重新加载部分。添加代码以获得更好的解释

标签: ios swift uitableview delegates reloaddata


【解决方案1】:

viewDidAppear() 中添加 tableView.reloadData()。因为 viewDidLoad() 只在 Viewcontroller 创建时调用。

【讨论】:

    【解决方案2】:

    这样做。

    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
    }
    

    }

    如果你想在每次 viewController 出现时重新加载表格,请在 viewWillAppear() 函数中调用 self.tblView.reloadData()。

    【讨论】:

      【解决方案3】:

      别忘了,UI 只能在主线程中更新!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-21
        • 2014-10-22
        • 2021-07-18
        • 1970-01-01
        相关资源
        最近更新 更多