【问题标题】:swift auto refresh Cell快速自动刷新单元格
【发布时间】:2018-10-05 20:28:03
【问题描述】:

我从另一个 ViewController 添加或删除 我在 tableView 中显示这个 array.count 我如何快速自动更新array.count的单元格? 没有 Timer 和 Pull 刷新

或者当loadedCart.count 发生变化时如何进行刷新? 谢谢

class TestTABLEVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableViewT: UITableView!

var CellT = TestTableViewCell()
var def = UserDefaults.standard
var loadedCart = [[String:Any]]()


override func viewDidLoad() {
    super.viewDidLoad()

    tableViewT.estimatedRowHeight = 100

    tableViewT.dataSource = self
    tableViewT.delegate = self

    loadedCart = UserDefaults.standard.array(forKey: "cartt") as? [[String: Any]] ?? [] 

   //Here need add auto update
 }

行的单元格

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TestTableViewCell

    let item = loadedCart[indexPath.row]
    cell.nameLbl?.text = item["name"] as? String
    cell.priceLbl.text = item["price"] as? String
    cell.qntLbl.text = item["qty"] as? String
    let im = item["image"] as? NSData
    cell.PhoroUmage.image = UIImage(data: im! as Data)



    return cell
}

Btn,点击时 - 删除 arr 并重新加载单元格

@IBAction func Btn(_ sender: UIButton) {
    def.removeObject(forKey: "cartt")
    print("remove is OK")
  //Here need add auto update when btn pressed
}

【问题讨论】:

  • 你的意思是 tableViewT.reloadData() 吗?
  • reloadData 无法自动更新我的单元格

标签: arrays swift uitableview io reload


【解决方案1】:

您可能需要使用通知。即当有新东西添加到您的购物车时发送通知。

然后设置一个观察者,每次观察者注意到变化时,用重新加载数据更新 tableview。

我认为这里列出了一个类似的用例,但需要根据您的需要进行调整(如果您需要更多帮助,比我更专业的人可能会在具体细节方面提供帮助!)Automatically Reload TableViewController On Rewind

【讨论】:

  • 我在 viewDidLoad 中添加通知,像这样 override func viewDidLoad() { super.viewDidLoad() tableViewT.estimatedRowHeight = 100 tableViewT.dataSource = self tableViewT.delegate = self loadedCart = UserDefaults.standard.array(forKey: "cartt") as? [[String: Any]] ?? [] NotificationCenter.default.addObserver(self, selector: "refreshTable:", name: NSNotification.Name(rawValue: "refresh"), object: nil) 并添加 func 但它不起作用
【解决方案2】:

如果我对您的理解正确,您可以为此使用反应式编程。例如Bond framework可以这样使用:

let todoItems: SafeSignal<[TodoItem]> = ....
let tableView: UITableView = ...
class TodoItemCell: UITableView Cell { ... }

...

todoItems.bind(to: tableView, cellType: TodoItemCell.self) { (cell, item) in
    cell.titleLabel.text = item.name
}

使用此框架,当源数组发生任何更改时,您的表格视图将自动重新加载。你可以在this link找到更多关于表格视图的使用。

【讨论】:

    【解决方案3】:

    添加

        override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        if let carts = UserDefaults.standard.array(forKey: "cartt") as? [[String: Any]] {
            loadedCart = carts
        }
        DispatchQueue.main.async{
            self.tableViewT.reloadData()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2013-06-11
      • 2015-06-29
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      相关资源
      最近更新 更多