Mike
Mike

Reputation: 7

Why TableView is empty

There are two ViewControllers in the app. The first one has a WebView and a Navigation Bar on top, the second one is a TableView to show URLs visited. Please see screenshots and codes. After I clicked Save button to save current URL user is visiting, the TableView is empty. I have tested the TableView first, it is set up fine. Please help to find out the problem. By the way, var savedURLs = [String]( I didn't put the other half of bracket as it won't show correctly if I put it.)

WebView Controller

TableView Controller

import UIKit

class BrowserViewController: UIViewController {


  @IBOutlet weak var browser: UIWebView!


  @IBAction func saveButtonPressed(sender: AnyObject) {

    if let currentURL = browser.request?.URL?.absoluteString  {

        savedURLs.append(currentURL)

        NSUserDefaults.standardUserDefaults().setObject(savedURLs, forKey: "savedURLs")

    }

  }

   override func viewDidLoad() {
      super.viewDidLoad()

      let url = NSURL(string: "https://www.google.com")!

      let request = NSURLRequest(URL: url)

      browser.loadRequest(request)

  }

}

import UIKit

var savedURLs = [String](

class SavingViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

  @IBOutlet weak var savingTableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()

   savingTableView.delegate = self
    savingTableView.dataSource = self

    if NSUserDefaults.standardUserDefaults().objectForKey("savedURLs") != nil {

        savedURLs = NSUserDefaults.standardUserDefaults().objectForKey("savedURLs") as! [String]
    }

  }



  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
  }


  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return savedURLs.count
  }


  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     let cell = tableView.dequeueReusableCellWithIdentifier("SavingCell")

    cell!.textLabel?.text = savedURLs[indexPath.row]

    return cell!

  }


  override func viewDidAppear(animated: Bool) {

    savingTableView.reloadData()

  }
}

Upvotes: 0

Views: 176

Answers (3)

Deepali Verma
Deepali Verma

Reputation: 57

Try passing the data to the next view controller when you click on send button rather than using NSUserDefaults. Maintain an array by appending the URLs that you visit, then pass it to the next view controller when you click on send.

Upvotes: 0

ntoonio
ntoonio

Reputation: 3133

I think you should add this function:

func reloadTable() {
    if NSUserDefaults.standardUserDefaults().objectForKey("savedURLs") != nil {
        savedURLs = NSUserDefaults.standardUserDefaults().objectForKey("savedURLs") as! [String]
    }
    savingTableView.reloadData()
}

And call that one instad of savingTableView.reloadData()

Also debug the code by printing the value of variables at some points. For example the new data when you read from NSUserDefaults and the value of savedURLs in viewDidAppear.

If that doesn't work make sure you're using the right cell identifier:

let cell = tableView.dequeueReusableCellWithIdentifier("SavingCell")

Upvotes: 0

Rubiya Faniband
Rubiya Faniband

Reputation: 370

enter image description herePlease check the currentURL is not nil. Because of empty currentURL your tableview is empty. I suggest you to Saved the urls in NSuserdefault after webview loading finished completely. Hope this will help you.

Upvotes: 1

Related Questions