Reputation: 37
So I'm trying to create a tableview and a cell won't show up. Other than not appending anything to the recipes array, is there anything blatantly wrong with my code?
If this means anything, when I try to link the tableview to my code, it doesn't give me the opportunity to create an outlet. Github link here
I'm New to iOS programming so this might be a dumb question, sorry if that's the case.
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]
}
}
Thanks.
Upvotes: 0
Views: 93
Reputation: 37
I wasn't saving to coreData as the Recipe
class was created in coreData, and I wasn't saving anything from the createRecipe
function.
Here's the fixes
New Function:
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")
}
}
Revised 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]
}
And the viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
recipes = createRecipe()
getRecipes()
}
Upvotes: 0
Reputation: 910
Actually the problem is within your storyboard. Your controller is not ViewController
is a basic UIViewController
. So you need to change the type from UIViewController
to ViewController
and link your tableView
after. That's why I didn't work when you were trying to link it the first time. I uploaded an image to be clear.
Upvotes: 0
Reputation: 896
You need to assign Recipe Class Array in viewDidLoad and In cellForAt assign 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
}
Upvotes: 1
Reputation: 36427
Where are you calling createRecipe()
? Where are you adding recipes to your recipes
array? I think what you meant was to write recipes = createRecipe()
in your viewDidLoad
. Additionally to be sure your tableView is loaded with the latest data, add tableView.reloadData()
as well.
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
recipes = createRecipe()
tableView.reloadData()
}
Otherwise your recipe
is would be just an empty array which translates to 0 rows...
Upvotes: 0