Swift 2.无法获取UITableViewCell来填充

时间:2015-12-31 06:06:15

标签: ios uitableview swift2

我仍然非常擅长编码,我正在创建一个菜谱应用程序,认为它应该相当简单。我已经研究了一段时间,并且能够通过youtube视频,关于udemy的课程,以及我在这里找到的一些代码和github,偶然发现它。我现在遇到了障碍,我找不到答案。

我输入了两个食谱,当我运行我的应用程序时,我正在尝试获取食谱名称以填充tableview,但它仍然是空白的。我知道这很可能是简单的事情,我已经尝试过所有事情。我觉得它出现在" cell.textLabel?.text = recipe.name"代码行,但我不确定。这是我到目前为止的代码

            //Test
            InboxFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            items = InboxFolder.Items;
            foreach (object mailitem in items)
            {
                olMail = mailitem as Outlook.MailItem;
                if (olMail != null)
                {
                    Outlook.UserProperty upK = olMail.UserProperties["Processed"];
                    if (upK != null)
                        upK.Delete();
                    olMail.Save();
                }
            }
            //Test

我的食谱文件看起来像这样

import UIKit

class tableVC: UITableViewController {

  var recipes: [AnyObject] = []

  override func viewDidLoad() {
    super.viewDidLoad()

    let recipe0: Recipe = Recipe()
    recipe0.name = "Number 1"
    recipe0.ingredients = ["Pasta","Pasta Sauce","Hamburger"]
    recipe0.directions = "Cook pasta to directions on box, brown hamburger, add sauce to hamburger, dump on pasta"

    let recipe1: Recipe = Recipe()
    recipe1.name = "Number 2"
    recipe1.ingredients = ["all kinds of stuff","Then a dash of salt"]
    recipe1.directions = "enjoy"
  }

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

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

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    let recipe: Recipe = recipes[indexPath.row] as! Recipe

    cell.textLabel?.text = recipe.name

    return cell
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

  }
}

非常感谢任何帮助或指导。

3 个答案:

答案 0 :(得分:2)

您正在Recipe中创建一些viewDidLoad个对象,但实际上并未将它们添加到recipes数组中 -

override func viewDidLoad() {
    super.viewDidLoad()

    let recipe0: Recipe = Recipe()
    recipe0.name = "Number 1"
    recipe0.ingredients = ["Pasta","Pasta Sauce","Hamburger"]
    recipe0.directions = "Cook pasta to directions on box, brown hamburger, add sauce to hamburger, dump on pasta"

    let recipe1: Recipe = Recipe()
    recipe1.name = "Number 2"
    recipe1.ingredients = ["all kinds of stuff","Then a dash of salt"]
    recipe1.directions = "enjoy"

    self.recipies=[recipe0,recipe1]
}

答案 1 :(得分:0)

像这样修改你的类,更改数组配方只保存Recipe对象,并在创建它们后添加它们。

import UIKit

class tableVC: UITableViewController {

var recipes: [Recipe] = []

override func viewDidLoad() {
    super.viewDidLoad()

let recipe0: Recipe = Recipe()
recipe0.name = "Number 1"
recipe0.ingredients = ["Pasta","Pasta Sauce","Hamburger"]
recipe0.directions = "Cook pasta to directions on box, brown hamburger, add sauce to hamburger, dump on pasta"
recipes.append(recipe0)

let recipe1: Recipe = Recipe()
recipe1.name = "Number 2"
recipe1.ingredients = ["all kinds of stuff","Then a dash of salt"]
recipe1.directions = "enjoy"
recipes.append(recipe1)

}

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

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

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = recipes[indexPath.row].name

    return cell
  }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

  }
}

答案 2 :(得分:-2)

有多种可能出错的可能性。最好检查的地方是数据源。

 let recipe: Recipe = recipes[indexPath.row] as! Recipe
 if(recipe.name)
  cell.textLabel?.text = recipe.name