为什么我不tableViewController加载任何数据? [英] Why is my tableViewController not loading any data?

查看:165
本文介绍了为什么我不tableViewController加载任何数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林创建一个应用程序,其中一个的ViewController负载不同菜单的进入tableViewController不同的按钮。这些按钮由prepare为SEGUE和菜单的(阵列)链接由contentMode相连。 1:早餐菜单&安培; 2:午餐菜单。我从别人配发的帮助下设置此功能,但现在该表没有加载任何数据......该单元有3个标签,其中显示项目,信息和价格。它在选择contentMode的code范围内变化值。有谁看到我的code中的问题?非常感谢!

 进口的UIKit类foodMenuController:UIViewController的{覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()FUNC prepareForSegue(SEGUE:UIStoryboardSegue,发件人:AnyObject?){
让foodMenuController = segue.destinationViewController的! foodTableViewController  如果segue.identifier ==showBreakfast{
            foodMenuController.contentMode = 1
    }否则,如果segue.identifier ==showLunch{
        foodMenuController.contentMode = 2
        }
    }
}覆盖FUNC didReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
    可以重新创建任何资源的处置//。
}
}


 进口的UIKit类foodTableViewCell:{的UITableViewCell@IBOutlet弱VAR foodItem:的UILabel!
@IBOutlet弱VAR foodDescription:的UILabel!
@IBOutlet弱VAR foodPrice:的UILabel!覆盖FUNC awakeFromNib(){
    super.awakeFromNib()
    //初始化code
}覆盖FUNC的setSelected(选择:BOOL,动画:BOOL){
    super.setSelected(选择动画:动画)    //配置视图中选择状态
}}


 进口的UIKit类foodTableViewController:{的UITableViewController在菜单//内容模式选择
VAR contentMode = 0//这应该是加载时内容模式是1 - >早餐
让breakfastItems =面包,咖啡,滩]
让breakfastInfo = [好,好,没有]
让breakfastPrice =$ 1,$ 100,$ 12,40]//这应该是加载时内容模式为2 - >午餐
让lunchItems =不是面包,不是咖啡,东西]
让lunchInfo = [不好,不好听,是]
让lunchPrice =$ 1,$ 100,$ 12,40]VAR foodItems:[字符串] = []
VAR foodInfo:[字符串] = []
VAR foodPrice:[字符串] = []覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()
    }覆盖FUNC viewWillAppear中(动画:BOOL){
    super.viewWillAppear(真)
    开关(contentMode){
    案例1:contentMode = 1
        foodItems = breakfastItems
        foodInfo = breakfastInfo
        foodPrice = breakfastPrice
    案例2:contentMode = 2
        foodItems = lunchItems
        foodInfo = lunchInfo
        foodPrice = lunchPrice
    默认:
        打破
    }
    tableView.reloadData()
}
覆盖FUNC didReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
    可以重新创建任何资源的处置//。}//表视图的数据源    覆盖FUNC numberOfSectionsInTableView(的tableView:UITableView的) - GT; INT {
    返回1
    }    覆盖FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection部分:
        INT) - GT; INT {
        返回foodItems.count}
覆盖FUNC的tableView(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - GT; {的UITableViewCell    让cellIdentifier =细胞
    让细胞= tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath:indexPath)作为! foodTableViewCell    cell.foodItem.text = foodItems [indexPath.row]
    cell.foodDescription.text = foodInfo [indexPath.row]
    cell.foodPrice.text = foodPrice [indexPath.row]        回报细胞
    }
}



解决方案

我在你的项目中略作改动。
1.使的UINavigationController InitialViewController
2.使 FoodMenuController 的UINavigationController 的根

现在修改 FoodMenuController

  @IBOutlet弱VAR bakeryButton:UIButton的!
@IBOutlet弱VAR breakfastButton:UIButton的!
覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()
    self.navigationController?.navigationBarHidden =真//隐藏导航栏第一的ViewController
    self.bakeryButton.addTarget(个体经营,动作:bakeryButtonAction:forControlEvents:.TouchUpInside)
    self.breakfastButton.addTarget(个体经营,动作:breakfastButtonAction:forControlEvents:.TouchUpInside)
}FUNC bakeryButtonAction(发件人:UIButton的){
    performSegueWithIdentifier(showLunch,发件人:个体经营)
}FUNC breakfastButtonAction(发件人:UIButton的){
    performSegueWithIdentifier(showBreakfast,发件人:个体经营)
}覆盖FUNC prepareForSegue(SEGUE:UIStoryboardSegue,发件人:AnyObject?){
    让foodTableViewController:FoodTableViewController = segue.destinationViewController的! FoodTableViewController    如果segue.identifier ==showBreakfast{
        foodTableViewController.contentMode = 1
    }否则,如果segue.identifier ==showLunch{
        foodTableViewController.contentMode = 2
    }
}

你也可以在 FoodTableViewController

UINavigationBar的可见

 覆盖FUNC viewDidLoad中(){
        super.viewDidLoad()
        self.navigationController?.navigationBarHidden = FALSE
    }

PS:它始终是最好不要直接添加SEGUE到的UIButton 。您也可以从黄色按钮之上使用其添加到您 FoodMenuController 并指定SEGUE在 UIButtonAction 被解雇 performSegueWithIdentifier

Im creating an app where different buttons in a ViewController load different menu's into the tableViewController. The buttons are linked by a prepare for segue and the menu's (arrays) are linked by a contentMode. 1: breakfast menu & 2: lunch menu. I had allot of help from someone setting this up but now the table is not loading any data... The cell has 3 labels which display an item, info and price. It changes value within the code when a contentMode is selected. Does anyone see the problem in my code? thanks a lot!

import UIKit

class foodMenuController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let foodMenuController = segue.destinationViewController as! foodTableViewController

  if segue.identifier == "showBreakfast" {
            foodMenuController.contentMode = 1
    } else if segue.identifier == "showLunch" {
        foodMenuController.contentMode = 2
        }
    }


}

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


import UIKit

class foodTableViewCell: UITableViewCell {

@IBOutlet weak var foodItem: UILabel!
@IBOutlet weak var foodDescription: UILabel!
@IBOutlet weak var foodPrice: 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
}

}


import UIKit

class foodTableViewController: UITableViewController {

//Content Mode Selection in menu
var contentMode = 0

// THIS SHOULD BE LOADED WHEN CONTENT MODE is "1" --> BREAKFAST
let breakfastItems = ["Bread", "Coffee", "Nada"]
let breakfastInfo = ["Good", "Nice", "Nothing"]
let breakfastPrice = ["$1", "$100", "$12,40"]

// THIS SHOULD BE LOADED WHEN CONTENT MODE IS "2" --> LUNCH
let lunchItems = ["Not bread", "Not Coffee", "Something"]
let lunchInfo = ["Not good", "Not nice", "Yes"]
let lunchPrice = ["$1", "$100", "$12,40"]

var foodItems: [String] = []
var foodInfo: [String] = []
var foodPrice: [String] = []

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

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)
    switch (contentMode){
    case 1: contentMode = 1
        foodItems = breakfastItems
        foodInfo = breakfastInfo
        foodPrice = breakfastPrice
    case 2: contentMode = 2
        foodItems = lunchItems
        foodInfo = lunchInfo
        foodPrice = lunchPrice
    default:
        break
    }
    tableView.reloadData()
}


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

}

// Table view data source

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

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

}


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

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! foodTableViewCell

    cell.foodItem.text = foodItems[indexPath.row]
    cell.foodDescription.text = foodInfo[indexPath.row]
    cell.foodPrice.text = foodPrice[indexPath.row]

        return cell


    }
}


解决方案

I have made slight modifications in your project. 1. make the UINavigationController the InitialViewController 2. make the FoodMenuController the root of UINavigationController

Now modify your FoodMenuController

@IBOutlet weak var bakeryButton: UIButton!
@IBOutlet weak var breakfastButton: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBarHidden = true //hide navigationBar in first ViewController
    self.bakeryButton.addTarget(self, action: "bakeryButtonAction:", forControlEvents: .TouchUpInside)
    self.breakfastButton.addTarget(self, action: "breakfastButtonAction:", forControlEvents: .TouchUpInside)
}

func bakeryButtonAction(sender: UIButton) {
    performSegueWithIdentifier("showLunch", sender: self)
}

func breakfastButtonAction(sender: UIButton) {
    performSegueWithIdentifier("showBreakfast", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let foodTableViewController: FoodTableViewController = segue.destinationViewController as! FoodTableViewController

    if segue.identifier == "showBreakfast" {
        foodTableViewController.contentMode = 1
    } else if segue.identifier == "showLunch" {
        foodTableViewController.contentMode = 2
    }
}

Also you can make UINavigationBar visible in FoodTableViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBarHidden = false
    }

PS: It is always better not to add segue directly to a UIButton. Alternatively you can add it from the yellow button on top of your FoodMenuController and specify the segue to be fired in UIButtonAction using performSegueWithIdentifier

这篇关于为什么我不tableViewController加载任何数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆