ios - UITableView 只显示一个部分
问题描述
我有以下内容UIViewController
,其中包含UITableView
:
class Home : UIViewController {
override func loadView() {
self.view = HomeView()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
有了这个观点:
class HomeView: UIView {
let tableView = UITableView()
let delegate = TVDelegate()
let dataSource = TVDataSource()
override init(frame: CGRect) {
super.init(frame: frame)
createSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
createSubviews()
}
func createSubviews() {
setupTableView()
setupConstraints()
}
func setupTableView() {
tableView.estimatedRowHeight = 500
tableView.tableFooterView = UIView()
tableView.delegate = delegate
tableView.dataSource = dataSource
tableView.backgroundColor = UIColor.purple
tableView.register(TVCell.self, forCellReuseIdentifier: "tableViewCell")
}
func setupConstraints() {
tableView.prepareView()
self.addFullSizeView(item: tableView)
}
}
这是我的代表:
class TVDelegate : NSObject, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 500
}
}
和我的数据源:
class TVDataSource : NSObject, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? TVCell
{
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your Favourites"
}
}
但是,当我运行我的应用程序时,我只看到一个带有一个标题的部分,而不是看到 5 个部分。
解决方案
numberOfSections
是一个dataSource方法应该在这里
class TVDataSource : NSObject, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
}
发生的情况是 tableView 采用默认数字 1 ,TVDataSource
因为该方法是可选的,所以它没有在类中实现
推荐阅读
- sql-server - 选项(QUERYTRACEON 9481),动态 SQL 和 DBCC TRACEON 错误
- javascript - AJAX - 实时 CSS 样式的问题
- javascript - 无法在 HTMLDocument (Firebase) 中读取 null 的属性“电子邮件”
- python - Errno 13 - Windows 10 上的 Jupyter Notebook 拒绝权限
- c# - 从延迟加载模型创建 DbQuery
- amazon-cloudwatch - 从 Fargate 容器内运行 AWS 日志代理
- python-3.x - 如何将 datetime.datetime(YYYY,mm,dd,HH,MM,SS) 格式转换为十进制 YYYYmm.DDHH
- javascript - 用于禁用 youtube 视频的鼠标单击的 Javascript 代码
- php - 如何在 PHP 中使用 shell_exec 运行带有参数的 .sh 文件?
- php - 是否可以将 URL 传递的“PHP”变量作为 SQL 查询的比较元素传递?