这是代码,这是我的最终显示。我使用的是表视图对象,而不是表视图控制器(表视图控制器可以正常工作),因为我打算对背景进行某些处理。 idk为什么不显示示例文本?这可能真是愚蠢... 这是输出的图像: !https://drive.google.com/file/d/1tEWZUjWP-8FhZKDvuAQyVV2vkh2_dyRL/view?usp=sharing 这是我如何配置单元的图像(我还有另一个拆分视图控制器用于其他目的,但不要紧,因为它与问题无关):!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()
}
}
答案 0 :(得分:0)
您的表视图无法显示数据,因为尚未设置数据源和委托ViewController显示数据。
您应该在viewDidload()中注册tableview的委托和数据源 请遵循以下代码:
self.tableView.delegate =自我 self.tableView.dataSource = self