2017-10-16 94 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中的一個錯誤。試圖從我從未保存過的東西中提取保存的數據。感謝您的幫助! –