要解决表格列没有填充的问题,首先需要确定是控制器还是连接表的问题。下面是一些可能的解决方法和代码示例:
numberOfRowsInSection
和cellForRowAt
)正确返回数据。代码示例:
class MyTableViewController: UITableViewController {
var data = ["Item 1", "Item 2", "Item 3"]
override func viewDidLoad() {
super.viewDidLoad()
// 设置表格的数据源和代理
tableView.dataSource = self
tableView.delegate = self
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 返回数据的数量
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// 填充每个表格列的数据
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
代码示例:
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var data = ["Item 1", "Item 2", "Item 3"]
override func viewDidLoad() {
super.viewDidLoad()
// 设置表格的数据源和代理
tableView.dataSource = self
tableView.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 返回数据的数量
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// 填充每个表格列的数据
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
通过检查控制器和连接表的设置,通常可以解决表格列没有填充的问题。