这是代码,这是我的结果显示。我使用的是 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/