iOS TableView自定义单元格没有显示任何内容

问题描述 投票:1回答:4

我是iOS和Swift的新手,我正在尝试通过创建一个简单的Todo应用程序来学习一点。我遇到的问题是,无论我如何实现代码(遵循多个教程)和故事板,数据都不会显示,自定义单元格也不会自定义(即使我已经自定义,它看起来也是默认单元格的外观它)。我已经连接了我的delegatedataSource

编辑:我已经分配了重用标识符

TodosView.swift

import UIKit

class TodosView: UIViewController {
    @IBOutlet weak var todosTable: UITableView!

    var todos: [Todo] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        todosTable.delegate = self
        todosTable.dataSource = self

        self.addTodo()
    }

    func addTodo() {
        let todo1 = Todo(text: "My first todo")
        let todo2 = Todo(text: "My second todo")

        todos = [todo1, todo2]
    }
}

extension TodosView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return todos.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let todo = todos[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodoCell") as! TodoCell

        cell.setTodo(todo: todo)

        return cell
    }
}

TodoCell.swift

import UIKit

class TodoCell: UITableViewCell {
    @IBOutlet weak var todoText: UILabel!

    func setTodo(todo: Todo) {
        todoText.text = todo.text
    }
}

Todo.swift

import Foundation

struct Todo {
    var text: String
    var done: Bool

    init(text: String, done: Bool = false) {
        self.text = text
        self.done = done
    }
}
ios swift uitableview
4个回答
5
投票

我成功地使用你的代码来成功生成你的Todo行(在调用reloadData()后我没有addTodo());

enter image description here

事实证明,您的代码确实有效,这让我相信您在Storyboard设置中的某个地方存在问题,而不是您在代码本身中遇到的问题。一些建议:

验证您的自定义单元是否已作为TodoCell进行子类化。您可以通过在Interface Builder中单击TodoCell来执行此操作,并在Identity Inspector选项卡中验证是否已将此设置设置为TodoCell:

enter image description here

这可能不是问题,因为如果您的单元格没有正确地进行子类化,您的应用程序很可能会崩溃。

验证您是否在Interface Builder中设置了单元标识符。再次,在Interface Builder中单击TodoCell,转到Attributes Inspector选项卡,并验证标识符是否设置为TodoCell:

enter image description here

另外,请确保您已将tableView和todoText UILabel实际连接到您的代码。我看到你有@IBOutlets这些项目,但如果你从教程中复制和粘贴,你可能输入项目,但从未真正连接它们。应填写tableView和UILabel的IBOutlet旁边的灰色圆圈,如下所示:

enter image description here

如果它是空的,您可能没有连接,这可以解释问题。我再次复制并粘贴您的代码,并根据上述建议设置内容;我不相信reloadData()或设置部分的数量将有助于解决问题(因为你的代码没有它们,而且它正在我的工作中)。


0
投票

更新数据源后需要重新加载tableview: -

func addTodo() {
    let todo1 = Todo(text: "My first todo")
    let todo2 = Todo(text: "My second todo")
    todos = [todo1, todo2]
    todosTable.reloadData()
}

编辑我还注意到,通过查看JWC注释,您没有实现numberOfSection方法,因此您还必须添加section delegate方法的数量。


0
投票

您在创建array后将数据添加到tableView

改变这一行:

var todos: [Todo] = []

var todos: [Todo]! {
    didSet {
        self.todosTable.reloadData()
    }
}

numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard (todos != nil) else {
        return 0
    }
    return todos.count
}

0
投票

你可以用它

func addTodo() {
   let todo1 = Todo(text: "My first todo")
   let todo2 = Todo(text: "My second todo")
   todos = [todo1, todo2]
   DispatchQueue.main.async {
      self.todosTable.reloadData()
   }
}

这可能对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.