Mark Gong-Guy
Mark Gong-Guy

Reputation: 45

Swift - Table View not populating with data but shows no errors

Using the following tutorial I have attempted to display some stock information that would populate in a table. I have created both the table view controller as well as the table view cell and linked stockTableViewCell with the table view cell. However I am unable to link the tableview on the Main storyboard with the custom table view controller.

Basically every time I try to enter it into the custom class field it does not apply and when I navigate away it disappears

My question is, is there something wrong with my view controller or table view cell or is there something in my storyboard that I have misconfigured?

Tutorial: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson7.html

Table View Controller

import UIKit

class stocksTableViewController: UITableViewController{

    // Mark: Properties

    var stocks = [stockData]()
    let stock = stockinfo()

    override func viewDidLoad() {
        super.viewDidLoad()
        loadSampleStockData()

    }

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

    func loadSampleStockData () {

        var stock1: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")
        var stock2: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")
        var stock3: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")

        stock.getInfo("FB") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
            stock1 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "FB")})
        }

        stock.getInfo("MSFT") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
            stock2 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "MSFT")})
        }

        stock.getInfo("APPL") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
            stock3 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "APPL")})
        }

        stocks += [stock1, stock2, stock3]
    }

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

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

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

        let cellIdentifier = "stockViewCell"

        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! stockTableViewCell

        let stock = stocks[indexPath.row]

        cell.stockName.text = stock.name
        cell.stockPercentage.text = stock.percentageChange
        cell.stockDollarChange.text = stock.askPrice
        cell.stockTicker.text = stock.stockTicker

        return cell
    }
}

Table View Cell

import Foundation
import UIKit

class stockTableViewCell: UITableViewCell {

    // Properties

    @IBOutlet weak var stockTicker: UILabel!
    @IBOutlet weak var stockPercentage: UILabel!
    @IBOutlet weak var stockName: UILabel!
    @IBOutlet weak var stockDollarChange: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization Code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}

Upvotes: 0

Views: 865

Answers (3)

GeraldoBastos
GeraldoBastos

Reputation: 313

I saw your project and I've some points that can help you:

  1. You don't have the outlet for your search field. You inserted the UISearchDisplayController but not connected with your class AddStockViewController.
  2. and most important UISearchDisplayerController is deprecated try to use the UISearchController or UISearchBar.

o/

Upvotes: 0

zsteed
zsteed

Reputation: 97

Not sure what issues you are having without looking at your project but what are you trying to enter into the custom class? You should enter stocksTableViewController and it should work, unless you didn't drag in a TableViewController on your storyboard. And you don't need to register your cell in the viewDidLoad ignore that comment.

Here use this updated code, your are trying to set up two types of view controllers to one view on your storyboard put this code into your class dashboardViewController and this should get you on the right track, don't forget to connect the outlet for the tableView property on this code when you implement it.

class dashboardViewController: DefaultViewController,     UITableViewDataSource, UITableViewDelegate {

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

  var stocks = [stockData]()
  let stock = stockinfo()

      override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    loadSampleStockData()
    user.newUser() // Move to login function when login and registration is implemented

//Sets the Balance Label on Dashboard
balanceLabel.text = "$" + String(format: "%.2f", user.getBalance())
  }

 func loadSampleStockData () {

var stock1: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")
var stock2: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")
var stock3: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")

stock.getInfo("FB") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
  stock1 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "FB")})
    }

stock.getInfo("MSFT") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
  stock2 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "MSFT")})
}

stock.getInfo("APPL") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
  stock3 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "APPL")})
}

stocks += [stock1, stock2, stock3]
  }

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

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

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

let cellIdentifier = "stockViewCell"

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! stockTableViewCell

let stock = stocks[indexPath.row]

cell.stockName.text = stock.name
cell.stockPercentage.text = stock.percentageChange
cell.stockDollarChange.text = stock.askPrice
cell.stockTicker.text = stock.stockTicker

return cell
    }`
  }

Upvotes: 0

Ramcharan Reddy
Ramcharan Reddy

Reputation: 689

You actually need to register your customtableviewcell in viewDidLoad()

Further you can also check the following link

http://www.mysamplecode.com/2013/06/ios-custom-uitableviewcell-example.html

Upvotes: 1

Related Questions