1

which I add or remove from another ViewController Im display this array.count in tableView How in swift I can auto updates cell for array.count? without Timer and Pull to refresh

Or how can I make refresh when loadedCart.count haw change? Thanks

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
 }

cell for row

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, when click - remove arr and reload cells

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

3 Answers3

0

you probably need to use notifications. i.e. send a notification when something new gets added to your cart.

Then set up an observer and, every time the observer notices a change, update the tableview with reload data.

A similar use case is listed here I think but would need to be adapted for your needs (if you need more help someone more expert than me will be able to help probably with the specifics!) Automatically Reload TableViewController On Rewind

nc14
  • 539
  • 1
  • 8
  • 26
  • Im add notifications in viewDidLoad like this `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)` And add func but its not work –  Apr 25 '18 at 13:47
0

If I understand you correctly, you can use reactive programming for this. For example Bond framework can be used like this:

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
}

Using this framework, your table view will automatically reload when there are any changes to the source array. You will find more about usage on table views at this link.

Fiser33
  • 286
  • 1
  • 10
0

Add

    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()
    }
}
Bogdan59
  • 31
  • 9