Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update data in tableview (Swift)

I have two Swift files: NotificationsViewController and ViewController. The firstArray is updated in the ViewContoller when a button is tapped. I was able to print the updated data. However, when I switch back to NotificationsViewController the tableview is not updated when I pull to refresh.

NotificationsViewController:

import Foundation
import UIKit

var  firstArray : [String] = ["123","1234"]

class NotificationsViewController: UITableViewController {


    var refresher: UIRefreshControl!

    var data: [[String]] = [firstArray, ["4","5"]]

    func refresh(){

        print("refreshed")
        self.tableView.reloadData()
        self.refresher.endRefreshing()

    }

    override func viewDidLoad() {


        refresher = UIRefreshControl()
        refresher.attributedTitle = NSAttributedString(string: "pull to refresh")

        refresher.addTarget(self, action: #selector(NotificationsViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
        self.tableView.addSubview(refresher)
        refresh()


        super.viewDidLoad()
        self.tableView.editing = true

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return data.count

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



        return data[section].count

    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "tableCell")

        cell.textLabel?.text = data[indexPath.section][indexPath.row]

        return cell
    }

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {

            data[indexPath.section].removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)



        }
    }

}

ViewController:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func button(sender: AnyObject) {
              firstArray.append("522")
              print(firstArray)

    }


}

I tried this but it did not work too.

Updated :

How can I update a value of cell.detailTextLabel?.text that is based on another array?

Thank you in advance

like image 577
Jana Avatar asked May 10 '16 18:05

Jana


People also ask

How do I create a uitableviewcontroller from a Tableview?

In the storyboard, drag & drop a tableview into the main view for the view controller (or remove the UIViewController and replace it with a UITableViewController) Add a prototype cell to the tableview with the subtitle style and set the reuse identifier to “Cell”

How to create an iOS single view app with Swift?

Let's jump straight into the coding part, but first: start Xcode, create a new iOS single view app project, enter some name & details for the project as usual, use Swift and finally open the ViewController.swift file right away. Now grab your keyboard! ⌨️

How to create a table view programmatically in iOS?

How to create a table view programmatically? Let's jump straight into the coding part, but first: start Xcode, create a new iOS single view app project, enter some name & details for the project as usual, use Swift and finally open the ViewController.swift file right away. Now grab your keyboard! ⌨️

Should I use reloaddata for my UITableView?

Doing this is fine if your data is not too big but the major drawback for using reloadData is that it will reload the entire UITableView which is not efficient. In Step 2 we will look at another way to add a row to our UITableView which is way more efficient and it is a better experience for the user.


1 Answers

One Issue:

After you update the first array. You need to update the data too. It doesn't get updated automatically. so your code could look like

func refresh(){
    data = [firstArray, ["4","5"]]
    print("refreshed")
    self.tableView.reloadData()
    self.refresher.endRefreshing()
}

Feedback:

Instead of using a variable outside of classes, its better to use a singleton class. It looks like this:

class Singleton: NSObject {
    static let sharedInstance = Singleton()
    var firstArray = []
}

You can update/retrive the array like

Singleton.sharedInstance.firstArray
like image 118
Subash Avatar answered Oct 04 '22 04:10

Subash