我用UIKit编程。我有一个UITableView
在另一个视图控制器中工作正常,我已经把我在那里使用的代码大部分复制到这个VC中,但我没有得到任何错误,UITableView
也没有显示。我检查了调试视图层次结构,它也不存在。我不确定这是否相关,但在启动调试视图层次结构时,调试窗口中会重复显示以下消息:[API] Failed to get renderer info (client=0x82b3d507) [0x5 (os/kern) failure]
。我在numberOfRowsInSection
中的print
语句会触发,但在cellForRowAt
中的print
语句不会触发。WorkoutTemplate
及其子结构是没有逻辑的简单数据结构。我在WorkoutTemplate上有一个静态属性,用于在布局UI时使用虚拟数据。
class CreateWorkoutVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
var exerciseListing: UITableView!
var workoutTemplate: WorkoutTemplate!
init(workoutTemplate: WorkoutTemplate) {
super.init(nibName: nil, bundle: nil)
self.workoutTemplate = workoutTemplate
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
}
private func configure() {
let padding: CGFloat = 20
exerciseListing = UITableView(frame: view.frame)
view.addSubview(exerciseListing)
exerciseListing.register(UITableViewCell.self, forCellReuseIdentifier: "exercise")
exerciseListing.rowHeight = 50
exerciseListing.dataSource = self
exerciseListing.delegate = self
exerciseListing.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
exerciseListing.topAnchor.constraint(equalTo: view.topAnchor, constant: padding),
exerciseListing.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
exerciseListing.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding)
])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("In numberOfRowsInSection: \(workoutTemplate.exerciseSets.count)")
return workoutTemplate.exerciseSets.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "exercise", for: indexPath)
let cellLabel = getCellLabel(withText: workoutTemplate.exerciseSets[indexPath.row].exercise.exerciseName)
print(workoutTemplate.exerciseSets[indexPath.row].exercise.exerciseName)
cell.addSubview(cellLabel)
let padding: CGFloat = 8
NSLayoutConstraint.activate([
cellLabel.centerYAnchor.constraint(equalTo: cell.centerYAnchor),
cellLabel.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: padding),
cellLabel.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -padding),
cellLabel.heightAnchor.constraint(equalTo: cell.heightAnchor)
])
cell.backgroundColor = .secondarySystemBackground
return cell
}
func getCellLabel(withText labelText: String) -> UILabel {
let cellLabel = UILabel(frame: .zero)
cellLabel.text = labelText
cellLabel.textColor = .label
cellLabel.adjustsFontSizeToFitWidth = true
cellLabel.minimumScaleFactor = 0.90
cellLabel.lineBreakMode = .byTruncatingTail
cellLabel.translatesAutoresizingMaskIntoConstraints = false
cellLabel.textAlignment = .left
cellLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
return cellLabel
}
}
数据结构:
struct WorkoutTemplate {
let workoutName: String
var exerciseSets: [ExerciseSet]
mutating func addExerciseSet(exerciseSet: ExerciseSet) {
exerciseSets.append(exerciseSet)
}
static let exampleWorkouts = [WorkoutTemplate(workoutName: "Upper Body 1", exerciseSets: [ExerciseSet(exercise: Exercise(exerciseId: UUID(), exerciseName: "Bench Press"), sets: 3, targetReps: 8, targetRPE: 8, restPeriod: 2), ExerciseSet(exercise: Exercise(exerciseId: UUID(), exerciseName: "T-Bar Row"), sets: 3, targetReps: 8, targetRPE: 8, restPeriod: 3)]), WorkoutTemplate(workoutName: "Lower Body 1", exerciseSets: [ExerciseSet(exercise: Exercise(exerciseId: UUID(), exerciseName: "Hack Squats"), sets: 3, targetReps: 8, targetRPE: 8, restPeriod: 2), ExerciseSet(exercise: Exercise(exerciseId: UUID(), exerciseName: "Leg Ext"), sets: 3, targetReps: 8, targetRPE: 8, restPeriod: 3)])]
}
struct ExerciseSet {
let exercise: Exercise
let sets: Int
let targetReps: Int
let targetRPE: Int
let restPeriod: Int
}
struct Exercise {
let exerciseId: UUID
let exerciseName: String
}
1条答案
按热度按时间nwsw7zdq1#
你说:
我检查了调试视图层次结构,它也不存在。
你签入了图形化表示?还是签入了左侧面板中的层次结构?我这样问是因为你似乎从未设置过底部约束,所以它的高度可以是任何值(甚至是零)。这将导致
numberOfRowsInSection
被调用,但cellForRowAt
不会被调用。底线是,表格视图是零高度,你不会在线框中看到它(或者它看起来是一条水平线)。这就是为什么我建议在左边面板中的视图层次结构中导航,找到表格视图。线框在调试你可以看到的视图的位置方面很好,但是零高度视图在那里很难分析。