2017-10-16 102 views
0

所以我想创建一个tableview和单元格不会显示出来。除了不向食谱数组添加任何东西,有没有什么公然错误的代码?UITableView将不会显示信息从阵列

如果这意味着什么,当我尝试将tableview链接到我的代码时,它不会给我创建插座的机会。 Github link here

我是新来iOS编程,所以这可能是一个愚蠢的问题,对不起,如果是这样的话。

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tableView: UITableView! 


var recipes : [Recipe] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.dataSource = self 
    tableView.delegate = self 


} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    let recipe = recipes[indexPath.row] 
    cell.textLabel?.text = recipe.title! 

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


func createRecipe() -> [Recipe] { 
    let recipe1 = Recipe() 
    recipe1.title = "Test" 
    recipe1.instructions = "testing" 
    recipe1.time = "testing" 

    let recipe2 = Recipe() 
    recipe2.title = "Test2" 
    recipe2.instructions = "testing" 
    recipe2.time = "testing" 

    return [recipe1, recipe2] 
} 



} 

谢谢。

+0

你已经解决了你的错误? – Hitesh

+0

@Hitesh是的,问题是我试图从coreData中拉出而不保存任何内容到coreData。我在下面回答了我自己的问题。感谢您的提交! –

回答

0

我没有保存到coreData,因为Recipe类是在coreData中创建的,我没有保存createRecipe函数中的任何内容。

这里的修复

新功能:

func getRecipes() { 
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

    do { 
     recipes = try context.fetch(Recipe.fetchRequest()) as! [Recipe] 
     print(recipes) 
    } catch { 
     print("ERROR") 
    } 

} 

修订createRecipe()

func createRecipe() -> [Recipe] { 

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
    let recipe3 = Recipe(context: context) 
    recipe3.title = "test3" 
    recipe3.instructions = "testing" 
    recipe3.time = "testing" 
    (UIApplication.shared.delegate as! AppDelegate).saveContext() 
    navigationController!.popViewController(animated: true) 


    return [recipe3] 
} 

而且viewDidLoad中

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.dataSource = self 
    tableView.delegate = self 

    recipes = createRecipe() 
    getRecipes() 
} 
0

你打电话给createRecipe()?你在哪里添加食谱到你的recipes阵列?我想你的意思是在你的viewDidLoad中写recipes = createRecipe()。另外为了确保你的tableView加载了最新的数据,加入tableView.reloadData()

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.dataSource = self 
    tableView.delegate = self 

    recipes = createRecipe() 
    tableView.reloadData() 
} 

否则你recipe是将只是一个空数组它转换为0 ...行

+0

原来,这主要是coreData中的一个错误。试图从我从未保存过的东西中提取保存的数据。感谢您的帮助! –

1

您需要在viewDidLoad中和在cellForAt分配的UITableViewCell

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.dataSource = self 
    tableView.delegate = self 

    recipes = createRecipe() 
    tableView.reloadData() 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 
    let recipe = recipes[indexPath.row] 
    cell.textLabel?.text = recipe.title! 

    return cell 
} 
+0

原来,这主要是coreData中的一个错误。试图从我从未保存过的东西中提取保存的数据。感谢您的帮助! –

0

分配食谱类Array其实问题在你的故事板内。您的控制器不是ViewController是基本的UIViewController。因此,您需要将类型从UIViewController更改为ViewController,然后将您的tableView链接。这就是为什么当你第一次尝试链接它时我没有工作。我上传了一张清晰的图片。 enter image description here

+0

原来,这主要是coreData中的一个错误。试图从我从未保存过的东西中提取保存的数据。感谢您的帮助! –