【发布时间】:2023-03-11 21:56:01
【问题描述】:
我有一些设计,其中父母和他们的孩子都是可折叠的。我已经使用表格视图实现了父级,但是对于它的子级,我将如何实现它的扩展单元格。我应该加载另一个自定义视图单元格还是应该更改当前单元格,我不知道。如果有人对此有任何想法,请帮助我。
// Model Class
class WorkoutTemplate {
var folderName:String
var workoutType: [WorkoutType]
init(folderName: String, workoutType: [WorkoutType]) {
self.folderName = folderName
self.workoutType = workoutType
}
}
class WorkoutType {
var workoutCategoryName: String // Basic Full Body
var totalWorkouts : [Workout]
init(workoutCategoryName: String, totalWorkouts: [Workout] ) {
self.workoutCategoryName = workoutCategoryName
self.totalWorkouts = totalWorkouts
}
}
class Workout{
var workoutName: String
var reps:Int = 0
var sets:Int = 0
init(workoutName: String, reps: Int, sets: Int) {
self.workoutName = workoutName
self.reps = reps
self.sets = sets
}
}
// Controller class
import UIKit
class AddWorkoutViewController: UIViewController {
@IBOutlet weak var tableView: UITableView?
var data = [WorkoutTemplate]()
var sectionIsExpanded = [false, false, false]
private let headerIdentifier = "WorkoutTemplateHeaderCell"
private let cellIdentifier = "AddWorkoutCategoryCell"
override func viewDidLoad() {
super.viewDidLoad()
let workout = Workout(workoutName: "Squat", reps: 10, sets: 9)
let workout2 = Workout(workoutName: "Bench press", reps: 6, sets: 7)
let workout3 = Workout(workoutName: "Dunmbell lunge", reps: 12, sets: 78)
let workoutType = WorkoutType(workoutCategoryName: "Basic full body", totalWorkouts: [workout, workout2, workout3])
let workoutTemplate = WorkoutTemplate(folderName: "My Workout Template", workoutType: [workoutType, workoutType])
data.append(workoutTemplate)
data.append(workoutTemplate)
data.append(workoutTemplate)
tableView?.reloadData()
}
}
extension AddWorkoutViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// First will always be header
let sectiondata = data[section]
return sectionIsExpanded[section] ? (sectiondata.workoutType.count + 1) : 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let headerCell = tableView.dequeueReusableCell(withIdentifier: headerIdentifier, for: indexPath) as! AddWorkoutTemplateHeaderTableViewCell
let sectionData = data[indexPath.section]
headerCell.headingLabel?.text = sectionData.folderName
if sectionIsExpanded[indexPath.section] {
headerCell.setExpanded()
} else {
headerCell.setCollapsed()
}
return headerCell
} else {
let catCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! AddWorkoutCategoryTableViewCell
let sectionData = data[indexPath.section]
let rowData = sectionData.workoutType[(indexPath.row - 1)]
catCell.headingLabel?.text = rowData.workoutCategoryName
var subHeadings = ""
for item in rowData.totalWorkouts {
subHeadings = subHeadings + item.workoutName + ", "
}
catCell.mutiWorkoutLabel?.text = subHeadings
return catCell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Expand/hide the section if tapped its header
if indexPath.row == 0 {
sectionIsExpanded[indexPath.section] = !sectionIsExpanded[indexPath.section]
tableView.reloadSections([indexPath.section], with: .automatic)
}
}
}
【问题讨论】:
标签: ios swift uitableview collapse expand