UITableView不会显示数组中的信息

时间:2017-10-16 15:10:28

标签: swift uitableview

所以我试图创建一个tableview,一个单元格不会出现。除了不向食谱数组添加任何内容之外,我的代码有什么明显的错误吗?

如果这意味着什么,当我尝试将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]
}



}

感谢。

4 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

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

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self

    recipes = createRecipe()
    tableView.reloadData()
}

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

答案 2 :(得分:0)

实际上问题出在你的故事板中。您的控制器不是$dir = read-host "Please Enter Directory to Search." $nameOfBadFile = Read-Host "The name of the bad file, including extension" $goodImg = read-host "Full location of your known good file (C:\somefile.txt)." $i=0; foreach($file in (Get-ChildItem $dir -Recurse | ?{$_.Name -eq $nameOfBadFile})) { $file.Delete() $i++; Write-Host "Just Deleted an instance of the bad image. You Have deleted $i images so far." $goodimg | Copy-Item -Destination $file.Directory Write-Host "Copied good file to current directory, $file.Directory" } 基本ViewController。因此,您需要将类型从UIViewController更改为UIViewController并在之后关联ViewController。这就是为什么当你第一次尝试链接它时我没有工作的原因。我上传的图片很清晰。enter image description here

答案 3 :(得分:0)

由于factor(c(2,1,1,3,3))类是在coreData中创建的,因此我没有保存到coreData,我也没有从Recipe函数中保存任何内容。

这里有修复

新功能:

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()

viewDidLoad

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]
}
相关问题