【问题标题】:tableView didset does not reloadtableView didset 不重新加载
【发布时间】:2019-12-28 17:31:19
【问题描述】:

每当调用 didset 时,我都会尝试重新加载 tableview。它从来没有工作过。

我已经尝试过警卫,但如果让在 didset 中,它永远不会成功。 我意识到它正在调用 tableview 的另一个对象。很奇怪,

导入 UIKit

类 ViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {

// Global Variable
var tableView: UITableView!
var tableView2 : UITableView!


var countRow : Int = 0  {
    didSet{


        //if let tableview  == tableview
         guard tableView2 == tableView else

        {

            tableView2 = UITableView(frame: self.view.bounds)
            tableView2.delegate = self
            tableView2.dataSource = self
           self.tableView2.reloadData()
            self.tableView.reloadData()
                print ("3countRow: \(countRow)")
            return
        }

        self.tableView2.reloadData()
        self.tableView.reloadData()
        print ("2countRow: \(countRow)")

    }

}

override func viewDidLoad() {
    super.viewDidLoad()



    tableView = UITableView(frame: self.view.bounds)
    tableView.delegate = self
    tableView.dataSource = self
    self.view.addSubview(tableView)
    tableView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)



    tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    tableView.register(AppCell.self, forCellReuseIdentifier: "NormalCell")
    tableView.register(appDays.self, forCellReuseIdentifier: "textField")
    tableView.reloadData()


}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tableView.reloadData()

}



func numberOfSectionsInTableView(tableView: UITableView) -> Int {


    return 3
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

print (countRow)
    return 5 + countRow


}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80

}




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    if indexPath.row == 0
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NormalCell", for: indexPath) as! AppCell
        cell.backgroundColor = UIColor.groupTableViewBackground
        return cell
    }

    if indexPath.row == 1 || indexPath.row == 2
    {
    var cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    cell.backgroundColor = UIColor.groupTableViewBackground
    return cell
    }

   let cell = tableView.dequeueReusableCell(withIdentifier: "textField", for: indexPath) as! appDays
    cell.backgroundColor = UIColor.groupTableViewBackground
    return cell



}

}

countrow 会在程序中打印出来

【问题讨论】:

    标签: tableview reload


    【解决方案1】:

    您永远不会将 countRow 设置为一个值。您只添加 5 + countRow 这并不意味着您在 countRow 中设置了 5。

    didSet 在新值存储后立即调用。 Swift official documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多