在tableview上添加数据
时间: 2023-03-23 15:00:22 浏览: 150
您可以使用以下步骤在 UITableView 上添加数据:
1.创建一个数组来保存要显示的数据。
2.在视图控制器中设置 UITableView,并将其数据源和委托设置为视图控制器。
3.实现 UITableViewDataSource 协议中的方法,包括返回表格行数和为每行提供单元格的方法。
4.在提供单元格的方法中,从数组中获取相应的数据并将其填充到单元格中。
5.将 UITableView 添加到视图中并加载数据。
示例代码:
// 创建数据数组
var dataArray = ["第一行数据", "第二行数据", "第三行数据"]
// 设置 UITableView
let tableView = UITableView(frame: view.bounds, style: .plain)
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
// 实现 UITableViewDataSource 协议方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
// 加载数据
tableView.reloadData()
阅读全文
相关推荐


















