tableview does not update





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















i'm trying to make a ordering food app for restaurants, when i add items to the cart for the first time table view loads the singleton array i've made. but when i go back to the menu and choose another item the array is updated but the table view of the cart doesn't, i'm using tabbarcontroller. tried to use tableview.reloadData() in different places still new data added to the array doesn't appear



class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var priceLbl: UILabel!
@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()

updateView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.cartItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as? CartCell{
let item = DataService.instance.cartItems[indexPath.row]
cell.configCell(cart: item)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
DataService.instance.cartItems.remove(at: indexPath.row)

tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
updateView()
tableView.endUpdates()
}
}

func updateView(){
var sum = 0
for item in DataService.instance.cartItems{
sum += item.itemPrice * item.quantity
print(item.itemPrice)
}
priceLbl.text = "$(sum)"
}

@IBAction func orderPressed(_ sender: Any) {
// upload order to firebase, clear cart and move to orderVC
}


}









share|improve this question

























  • @yourssef i in tabbar viewDidLoad called only once. Will you please check reload in viewWillAppear. Also check your array does your new value added in the array and then reload will called?

    – chirag shah
    Jan 3 at 8:51











  • After the modification of the property DataService.instance.cartItems, you need to reload your table view. You need to somehow notify (possibly delegation, callback closure) your CartVC where you will perform tableView.reloadData()

    – nayem
    Jan 3 at 8:51











  • @chiragshah were should i add viewwillappear?? in the cartVC or the mainTabBarVC. and yes new values are added to the array

    – youssef SULTAN
    Jan 3 at 9:03













  • @youssefSULTAN add in CartVC

    – chirag shah
    Jan 3 at 9:04











  • @chiragshah it worked thank you

    – youssef SULTAN
    Jan 3 at 9:13


















2















i'm trying to make a ordering food app for restaurants, when i add items to the cart for the first time table view loads the singleton array i've made. but when i go back to the menu and choose another item the array is updated but the table view of the cart doesn't, i'm using tabbarcontroller. tried to use tableview.reloadData() in different places still new data added to the array doesn't appear



class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var priceLbl: UILabel!
@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()

updateView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.cartItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as? CartCell{
let item = DataService.instance.cartItems[indexPath.row]
cell.configCell(cart: item)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
DataService.instance.cartItems.remove(at: indexPath.row)

tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
updateView()
tableView.endUpdates()
}
}

func updateView(){
var sum = 0
for item in DataService.instance.cartItems{
sum += item.itemPrice * item.quantity
print(item.itemPrice)
}
priceLbl.text = "$(sum)"
}

@IBAction func orderPressed(_ sender: Any) {
// upload order to firebase, clear cart and move to orderVC
}


}









share|improve this question

























  • @yourssef i in tabbar viewDidLoad called only once. Will you please check reload in viewWillAppear. Also check your array does your new value added in the array and then reload will called?

    – chirag shah
    Jan 3 at 8:51











  • After the modification of the property DataService.instance.cartItems, you need to reload your table view. You need to somehow notify (possibly delegation, callback closure) your CartVC where you will perform tableView.reloadData()

    – nayem
    Jan 3 at 8:51











  • @chiragshah were should i add viewwillappear?? in the cartVC or the mainTabBarVC. and yes new values are added to the array

    – youssef SULTAN
    Jan 3 at 9:03













  • @youssefSULTAN add in CartVC

    – chirag shah
    Jan 3 at 9:04











  • @chiragshah it worked thank you

    – youssef SULTAN
    Jan 3 at 9:13














2












2








2








i'm trying to make a ordering food app for restaurants, when i add items to the cart for the first time table view loads the singleton array i've made. but when i go back to the menu and choose another item the array is updated but the table view of the cart doesn't, i'm using tabbarcontroller. tried to use tableview.reloadData() in different places still new data added to the array doesn't appear



class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var priceLbl: UILabel!
@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()

updateView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.cartItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as? CartCell{
let item = DataService.instance.cartItems[indexPath.row]
cell.configCell(cart: item)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
DataService.instance.cartItems.remove(at: indexPath.row)

tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
updateView()
tableView.endUpdates()
}
}

func updateView(){
var sum = 0
for item in DataService.instance.cartItems{
sum += item.itemPrice * item.quantity
print(item.itemPrice)
}
priceLbl.text = "$(sum)"
}

@IBAction func orderPressed(_ sender: Any) {
// upload order to firebase, clear cart and move to orderVC
}


}









share|improve this question
















i'm trying to make a ordering food app for restaurants, when i add items to the cart for the first time table view loads the singleton array i've made. but when i go back to the menu and choose another item the array is updated but the table view of the cart doesn't, i'm using tabbarcontroller. tried to use tableview.reloadData() in different places still new data added to the array doesn't appear



class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var priceLbl: UILabel!
@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()

updateView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.cartItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as? CartCell{
let item = DataService.instance.cartItems[indexPath.row]
cell.configCell(cart: item)
return cell
} else {
return UITableViewCell()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
DataService.instance.cartItems.remove(at: indexPath.row)

tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
updateView()
tableView.endUpdates()
}
}

func updateView(){
var sum = 0
for item in DataService.instance.cartItems{
sum += item.itemPrice * item.quantity
print(item.itemPrice)
}
priceLbl.text = "$(sum)"
}

@IBAction func orderPressed(_ sender: Any) {
// upload order to firebase, clear cart and move to orderVC
}


}






ios swift4.2 xcode9.4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 8:42









Scriptable

13.5k43555




13.5k43555










asked Jan 3 at 8:35









youssef SULTANyoussef SULTAN

155




155













  • @yourssef i in tabbar viewDidLoad called only once. Will you please check reload in viewWillAppear. Also check your array does your new value added in the array and then reload will called?

    – chirag shah
    Jan 3 at 8:51











  • After the modification of the property DataService.instance.cartItems, you need to reload your table view. You need to somehow notify (possibly delegation, callback closure) your CartVC where you will perform tableView.reloadData()

    – nayem
    Jan 3 at 8:51











  • @chiragshah were should i add viewwillappear?? in the cartVC or the mainTabBarVC. and yes new values are added to the array

    – youssef SULTAN
    Jan 3 at 9:03













  • @youssefSULTAN add in CartVC

    – chirag shah
    Jan 3 at 9:04











  • @chiragshah it worked thank you

    – youssef SULTAN
    Jan 3 at 9:13



















  • @yourssef i in tabbar viewDidLoad called only once. Will you please check reload in viewWillAppear. Also check your array does your new value added in the array and then reload will called?

    – chirag shah
    Jan 3 at 8:51











  • After the modification of the property DataService.instance.cartItems, you need to reload your table view. You need to somehow notify (possibly delegation, callback closure) your CartVC where you will perform tableView.reloadData()

    – nayem
    Jan 3 at 8:51











  • @chiragshah were should i add viewwillappear?? in the cartVC or the mainTabBarVC. and yes new values are added to the array

    – youssef SULTAN
    Jan 3 at 9:03













  • @youssefSULTAN add in CartVC

    – chirag shah
    Jan 3 at 9:04











  • @chiragshah it worked thank you

    – youssef SULTAN
    Jan 3 at 9:13

















@yourssef i in tabbar viewDidLoad called only once. Will you please check reload in viewWillAppear. Also check your array does your new value added in the array and then reload will called?

– chirag shah
Jan 3 at 8:51





@yourssef i in tabbar viewDidLoad called only once. Will you please check reload in viewWillAppear. Also check your array does your new value added in the array and then reload will called?

– chirag shah
Jan 3 at 8:51













After the modification of the property DataService.instance.cartItems, you need to reload your table view. You need to somehow notify (possibly delegation, callback closure) your CartVC where you will perform tableView.reloadData()

– nayem
Jan 3 at 8:51





After the modification of the property DataService.instance.cartItems, you need to reload your table view. You need to somehow notify (possibly delegation, callback closure) your CartVC where you will perform tableView.reloadData()

– nayem
Jan 3 at 8:51













@chiragshah were should i add viewwillappear?? in the cartVC or the mainTabBarVC. and yes new values are added to the array

– youssef SULTAN
Jan 3 at 9:03







@chiragshah were should i add viewwillappear?? in the cartVC or the mainTabBarVC. and yes new values are added to the array

– youssef SULTAN
Jan 3 at 9:03















@youssefSULTAN add in CartVC

– chirag shah
Jan 3 at 9:04





@youssefSULTAN add in CartVC

– chirag shah
Jan 3 at 9:04













@chiragshah it worked thank you

– youssef SULTAN
Jan 3 at 9:13





@chiragshah it worked thank you

– youssef SULTAN
Jan 3 at 9:13












2 Answers
2






active

oldest

votes


















0














In tabbar every time viewDidLoad not called so you need to reload the data in viewWillAppear or viewDidAppear. Here is the reference.



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self

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

tableView.reloadData()

}





share|improve this answer































    0














    UITableViewDelegate is not reactive. You have to call reloadData() when you want to update the tableView content (when your DataService.instance value is updated).



    In a MVP world, your "add" action will trigger an event to the presenter. And the presenter sends back to the view an action to update the tableView, after updating your DataService.



    Maybe you should look at RxSwift/ReactiveCocoa, which provides APIs to automatically bind your DataService.instance array to the cells rendered in the UITableView.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018783%2ftableview-does-not-update%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      In tabbar every time viewDidLoad not called so you need to reload the data in viewWillAppear or viewDidAppear. Here is the reference.



      override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.
      tableView.delegate = self
      tableView.dataSource = self

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

      tableView.reloadData()

      }





      share|improve this answer




























        0














        In tabbar every time viewDidLoad not called so you need to reload the data in viewWillAppear or viewDidAppear. Here is the reference.



        override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self

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

        tableView.reloadData()

        }





        share|improve this answer


























          0












          0








          0







          In tabbar every time viewDidLoad not called so you need to reload the data in viewWillAppear or viewDidAppear. Here is the reference.



          override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
          tableView.delegate = self
          tableView.dataSource = self

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

          tableView.reloadData()

          }





          share|improve this answer













          In tabbar every time viewDidLoad not called so you need to reload the data in viewWillAppear or viewDidAppear. Here is the reference.



          override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
          tableView.delegate = self
          tableView.dataSource = self

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

          tableView.reloadData()

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 9:17









          chirag shahchirag shah

          1,9191644




          1,9191644

























              0














              UITableViewDelegate is not reactive. You have to call reloadData() when you want to update the tableView content (when your DataService.instance value is updated).



              In a MVP world, your "add" action will trigger an event to the presenter. And the presenter sends back to the view an action to update the tableView, after updating your DataService.



              Maybe you should look at RxSwift/ReactiveCocoa, which provides APIs to automatically bind your DataService.instance array to the cells rendered in the UITableView.






              share|improve this answer




























                0














                UITableViewDelegate is not reactive. You have to call reloadData() when you want to update the tableView content (when your DataService.instance value is updated).



                In a MVP world, your "add" action will trigger an event to the presenter. And the presenter sends back to the view an action to update the tableView, after updating your DataService.



                Maybe you should look at RxSwift/ReactiveCocoa, which provides APIs to automatically bind your DataService.instance array to the cells rendered in the UITableView.






                share|improve this answer


























                  0












                  0








                  0







                  UITableViewDelegate is not reactive. You have to call reloadData() when you want to update the tableView content (when your DataService.instance value is updated).



                  In a MVP world, your "add" action will trigger an event to the presenter. And the presenter sends back to the view an action to update the tableView, after updating your DataService.



                  Maybe you should look at RxSwift/ReactiveCocoa, which provides APIs to automatically bind your DataService.instance array to the cells rendered in the UITableView.






                  share|improve this answer













                  UITableViewDelegate is not reactive. You have to call reloadData() when you want to update the tableView content (when your DataService.instance value is updated).



                  In a MVP world, your "add" action will trigger an event to the presenter. And the presenter sends back to the view an action to update the tableView, after updating your DataService.



                  Maybe you should look at RxSwift/ReactiveCocoa, which provides APIs to automatically bind your DataService.instance array to the cells rendered in the UITableView.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 8:56









                  jtouzyjtouzy

                  19617




                  19617






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018783%2ftableview-does-not-update%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith