简体   繁体   English

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

[英]iOS TableView custom cell not showing anything

I'm new to iOS and Swift and I'm trying to learn a little by creating a simple Todo app. 我是iOS和Swift的新手,我想通过创建一个简单的Todo应用程序来学习一些知识。 The problem I came across is that no matter how I implement the code (followed multiple tutorials) and storyboards, the data doesn't show and the custom cells is not customized (it looks exactly how the default cells look even though I've customized it). 我遇到的问题是,无论我如何实现代码(接续多个教程)和情节提要,数据都不会显示,并且自定义单元格也不会自定义(即使我已自定义,它看起来也像默认单元格一样)它)。 I already connected my delegate and dataSource 我已经连接了我的delegatedataSource

Edit: I already assigned the reuse identifier 编辑:我已经分配了重用标识符

TodosView.swift 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 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
    }
}

I succeeded in using your code to successfully generate your Todo rows (I did not reloadData() after calling addTodo() ); 我成功地使用您的代码成功生成了您的Todo行(调用addTodo()之后,我没有reloadData() addTodo() );

在此处输入图片说明

Having proven that your code does work, it leads me to believe that you have an issue somewhere in your Storyboard setup, more-so than you do in your code itself. 证明您的代码确实有效后,我使我相信您在Storyboard设置中的某个地方比在代码本身中更容易遇到问题。 A few suggestions: 一些建议:

Verify your custom cell is subclassed as a TodoCell . 验证您的自定义单元是否被子类化为TodoCell You can do this by clicking on your TodoCell in Interface Builder, and in the Identity Inspector tab, verify you have this set to TodoCell: 您可以通过在Interface Builder中单击TodoCell来执行此操作,然后在Identity Inspector选项卡中,确认已将此设置为TodoCell:

在此处输入图片说明

This is likely not the issue as your app would more than likely crash if your cells were not subclassed properly. 这可能不是问题,因为如果单元的分类不正确,您的应用很可能会崩溃。

Verify you have set the cell identifier in Interface Builder . 验证您是否已在Interface Builder中设置了单元标识符 Again, click on the TodoCell in Interface Builder, go to the Attributes Inspector tab, and verify identifier is set to TodoCell: 再次,在Interface Builder中单击TodoCell,转到Attributes Inspector选项卡,并验证标识符是否设置为TodoCell:

在此处输入图片说明

Also, do make sure that you've actually connected your tableView and todoText UILabel to your code. 另外,请确保已将tableView和todoText UILabel确实连接到了代码。 I see you have @IBOutlets to these items, but if you were copying and pasting from a tutorial, it's possible you typed in the items and never actually connected them. 我看到您在这些项目上有@IBOutlets,但是如果您是从教程中复制和粘贴的,则有可能您键入了这些项目而从未真正连接它们。 The gray circle next to your IBOutlet for both the tableView and UILabel should be filled in, like so: 应该为tableView和UILabel填充IBOutlet旁边的灰色圆圈,如下所示:

在此处输入图片说明

If it's empty, you may not have a connection, which could explain the issue. 如果为空,则可能没有连接,这可以解释该问题。 Again, I copied and pasted your code verbatim and set things per the above suggestions; 再次,我逐字复制并粘贴了您的代码,并根据上述建议进行了设置; I do not believe that reloadData() or setting the number of sections will help the issue (as your code did not have them and it's working on my end). 我不相信reloadData()或设置节数可以解决这个问题(因为您的代码中没有它们,并且可以正常工作)。

You need to reload your tableview after updating the datasource :- 您需要在更新数据源后重新加载表格视图:-

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

Edit I also noticed by looking at the JWC comment you didn't have the numberOfSection method implemented so you must also add the number of section delegate method. 编辑通过查看JWC注释,我还注意到您尚未实现numberOfSection方法,因此还必须添加部分委托方法的数量。

You are adding the data to array after creating the tableView . 您将在创建tableView之后将数据添加到array

Change this line : 更改此行:

var todos: [Todo] = []

to

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

and in numberOfRowsInSection : 并在numberOfRowsInSection

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

you can use this 你可以用这个

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

This might help you 这可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM