Swift - 表格视图没有填充数据,但没有显示错误

时间:2016-07-01 17:49:47

标签: ios swift uitableview uiviewcontroller

使用以下教程,我试图显示一些将填充在表格中的股票信息。我创建了表视图控制器以及表视图单元,并将stockTableViewCell与表视图单元格链接在一起。但是,我无法将主故事板上的tableview与自定义表视图控制器链接。

基本上每当我尝试将其输入自定义类字段时,它都不适用,当我离开时它会消失

我的问题是,我的视图控制器或表格视图单元格是否有问题,或者我的故事板中是否存在我配置错误的内容?

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

表视图控制器

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
    }
}

表格视图单元格

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
    }
}

3 个答案:

答案 0 :(得分:1)

您实际上需要在 viewDidLoad()

中注册customtableviewcell

此外,您还可以查看以下链接

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

答案 1 :(得分:0)

如果不查看您的项目,不确定您遇到了什么问题,但是您想要进入自定义类的是什么?您应该输入stocksTableViewController并且它应该可以工作,除非您没有在故事板上拖动TableViewController。而且您不需要在viewDidLoad中注册您的单元格而忽略该注释。

这里使用这个更新的代码,您正在尝试将两种类型的视图控制器设置到故事板上的一个视图中将此代码放入您的类dashboardViewController中,这应该让您走在正确的轨道上,不要忘记连接实现它时,此代码上的tableView属性的出口。

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
    }`
  }

答案 2 :(得分:0)

我看到了你的项目,并且我提供了一些可以帮助你的要点:

  1. 您没有搜索字段的出口。您插入了UISearchDisplayController但未与您的AddStockViewController类连接。
  2. 不推荐使用
  3. 并且最重要的UISearchDisplayerController尝试使用UISearchController或UISearchBar。
  4. O /