Swift UITableView赢得';t显示来自数组的信息
所以我试图创建一个tableview,但单元格不会显示。除了没有向recipes数组添加任何内容之外,我的代码是否有任何明显的错误 如果这意味着什么的话,当我尝试将tableview链接到我的代码时,它并没有给我创建出口的机会 我是iOS编程新手,所以这可能是个愚蠢的问题,如果是这样的话,很抱歉Swift UITableView赢得';t显示来自数组的信息,swift,uitableview,Swift,Uitableview,所以我试图创建一个tableview,但单元格不会显示。除了没有向recipes数组添加任何内容之外,我的代码是否有任何明显的错误 如果这意味着什么的话,当我尝试将tableview链接到我的代码时,它并没有给我创建出口的机会 我是iOS编程新手,所以这可能是个愚蠢的问题,如果是这样的话,很抱歉 import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @
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]
}
}
谢谢。您在哪里呼叫
createRecipe()
?您在哪里向配方
数组添加配方?我想你的意思是在viewDidLoad
中编写recipes=createRecipe()
。此外,为了确保tableView加载了最新数据,请添加tableView.reloadData()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
recipes = createRecipe()
tableView.reloadData()
}
否则,您的
配方将只是一个空数组,转换为0行…您需要在viewDidLoad和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
}
事实上,问题就在你的故事板里。您的控制器不是ViewController
而是基本的UIViewController
。因此,您需要将类型从UIViewController
更改为ViewController
,然后链接tableView
。这就是为什么你第一次尝试链接时我没有工作。我上传了一个图像以保持清晰。我没有保存到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()
还有那辆车
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
recipes = createRecipe()
getRecipes()
}
你的bug解决了吗?@Hitesh是的,问题是我试图从coreData中提取,但没有将任何内容保存到coreData。我在下面回答了我自己的问题。谢谢你的提交!结果证明这主要是coreData中的错误。试图从我从未保存任何内容的内容中提取保存的数据。谢谢你的帮助!结果证明这主要是coreData中的错误。试图从我从未保存任何内容的内容中提取保存的数据。谢谢你的帮助!结果证明这主要是coreData中的错误。试图从我从未保存任何内容的内容中提取保存的数据。谢谢你的帮助!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
recipes = createRecipe()
getRecipes()
}