swift - 为什么文本不显示在 UItableview 对象上

标签 swift uitableview

这是代码,这是我的结果显示。我使用的是 TableView 对象,不是 TableView Controller ( TableView Controller 工作正常),因为我打算对背景做一些事情。不知道为什么不显示示例文本?这可能真的很愚蠢...... 这是输出的图像: ! https://drive.google.com/file/d/1tEWZUjWP-8FhZKDvuAQyVV2vkh2_dyRL/view?usp=sharing 这是我如何配置单元格的图像(我有另一个用于其他目的的 Split View Controller ,但不要介意,因为它与问题无关):! https://drive.google.com/file/d/1u-T1J6xp1bZq12HRCSeD3Eq_VWlGH4lb/view?usp=sharing

请帮忙,谢谢!我也试着看看 How to insert new cell into UITableView in Swift并使用 +21 响应

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    //MARK: Properties
    @IBOutlet weak var drawingCanvas: UIView!
    @IBOutlet weak var tableView: UITableView!
    var tableData = ["one"]

    //MARK: Table View
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == self.tableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath)
            // Configure the cell...
            cell.textLabel?.text = tableData[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }


}

最佳答案

您的 tableview 无法显示数据,因为您还没有为 ViewController 设置数据源和委托(delegate)来显示数据。

你应该在 viewDidload() 中注册 tableview 的委托(delegate)和数据源 请遵循以下代码:

self.tableView.delegate = self self.tableView.dataSource = self

关于swift - 为什么文本不显示在 UItableview 对象上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57238411/

相关文章:

ios - 得到 NSUnknownKeyException' 的错误,原因 : setValue:forUndefinedKey: this class is not key value coding-compliant for the key description

ios - Swift 如何将文档文件 URL 转换为字符串,以便在 tableview 中显示文件名

ios - 如何向此 UITableView 添加额外的列并填充已获取的数据?

swift - 1 个结构、3 个协议(protocol)和 1 个函数来调用它们

ios - 使用 switch 语句创建泛型函数,该函数根据传入参数的类型执行不同的代码

ios - UITextView 取消隐藏后更改 UITableViewCell

uitableview - 在 ViewController 和 Swift 中的条件 segue 之间传递选定的行

ios - 如何更改键盘的宽度

ios - 如何在 Swift 中向后发送 UIView 元素

swift - 定义与以前的值冲突 - 另外,何时删除覆盖?