tableView 无法在 UIView 中正确滚动
tableView not scrolling properly inside UIView
我有一个table程序化视图。我初始化了部分和行的大小。我还在情节提要中放置了一个 containerView,其中包含 tableView。我的问题是,我的 table 视图无法正确滚动。
这是我的故事板:
这是故事板中视图的出路:
@IBOutlet weak var trainingDetailsContainerView: UIView!
这是我初始化的 table:
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
}()
这里是我设置 table视图、高度和所有内容的地方:
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
中的 table 视图添加约束,而不是设置框架。同时从 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
}
我已经检查过 - 这个 ViewController 应该可以工作:
class ViewController: UIViewController {
@IBOutlet weak var trainingDetailsContainerView: UIView!
var moduleLessonsTableView: UITableView = {
let tableView = UITableView()
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
}()
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
}
}
我有一个table程序化视图。我初始化了部分和行的大小。我还在情节提要中放置了一个 containerView,其中包含 tableView。我的问题是,我的 table 视图无法正确滚动。
这是我的故事板:
这是故事板中视图的出路:
@IBOutlet weak var trainingDetailsContainerView: UIView!
这是我初始化的 table:
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
}()
这里是我设置 table视图、高度和所有内容的地方:
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
中的 table 视图添加约束,而不是设置框架。同时从 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
}
我已经检查过 - 这个 ViewController 应该可以工作:
class ViewController: UIViewController {
@IBOutlet weak var trainingDetailsContainerView: UIView!
var moduleLessonsTableView: UITableView = {
let tableView = UITableView()
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
}()
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
}
}