swift - tableView 在 UIView 内无法正确滚动
问题描述
我有一个以编程方式制作的 tableView。我初始化了部分和行的大小。我还在情节提要中放置了一个 containerView,其中包含 tableView。我的问题是,我的表格视图无法正确滚动。
这将是我的故事板:
这将是我在情节提要中查看视图的出路:
@IBOutlet weak var trainingDetailsContainerView: UIView!
这将是我的初始化表:
var moduleLessonsTableView: UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
// tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = Themes.gray_dec
tableView.sectionHeaderHeight = 35
tableView.rowHeight = 50
tableView.isScrollEnabled = true
tableView.register(TrainingModulesTableViewCell.self, forCellReuseIdentifier: "ModuleLessonsCell")
return tableView
}()
这里是我设置我的 tableView、高度和所有内容的地方:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
moduleLessonsTableView.frame = CGRect(x: 0, y: 0, width: trainingDetailsContainerView.frame.width, height: trainingDetailsContainerView.frame.height)
trainingDetailsContainerView.addSubview(moduleLessonsTableView)
}
我可以尝试将它放在 viewWillAppear 中并且高度会正确呈现,这让我意识到 trainingDetailsContainerView 的边界设置在其他视图之前,我想。但它的问题是它会突然弹出。我真的不希望这样,因为从用户的角度来看,它看起来像是一个滞后。
任何帮助将不胜感激。
我也不能标记视图生命周期,所以我就把它放在这里。
解决方案
尝试在表格视图中添加约束viewDidLoad
而不是设置框架。viewWillAppear
还要从方法中删除代码。示例代码:
override func viewDidLoad() {
super.viewDidLoad()
trainingDetailsContainerView.addSubview(moduleLessonsTableView)
moduleLessonsTableView.topAnchor.constraint(equalTo: trainingDetailsContainerView.topAnchor).isActive = true
moduleLessonsTableView.leadingAnchor.constraint(equalTo: trainingDetailsContainerView.leadingAnchor).isActive = true
moduleLessonsTableView.trailingAnchor.constraint(equalTo: trainingDetailsContainerView.trailingAnchor).isActive = true
moduleLessonsTableView.bottomAnchor.constraint(equalTo: trainingDetailsContainerView.bottomAnchor).isActive = true
}
推荐阅读
- python - 从字符列表中查找可能的单词而不使用字符两次
- android - viewPager 回调中的片段
- openpyxl - 如何解决 Openpyxl TypeError:预期
openpyxl-3.0.3 中的错误 - node.js - 发送除 GET 以外的 http 请求时无响应或无状态码
- uikit - SwiftUI 有没有更好的方法来删除 NavigationView 上的背景
- python - 在跳过上一次运行期间完成的迭代时如何使用 tqdm(剩余时间不正确)?
- php - 如何解决 Laravel 中的“函数 create_function() 已弃用”
- java - 我在gradle中有错误,我该怎么办?
- reactjs - 在反应中使用rest api将视频文件上传(.mp4)到服务器
- javascript - Javascript 输出一个包含两个值的数组,其中两个值用它们自己的变量分隔