我想用 tableview 应用骨架动画。为了实现这一点,我正在使用“SkeletonView”cocopods,它在单个部分中运行良好,但是当我尝试使用多个部分时,它会抛出越界错误。即使我不知道从哪里可以找到这个 cocopods 的文档。如果有人有任何想法,请帮助我。
import UIKit
import SkeletonView
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var data = [[String: Any]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 80
tableView.estimatedRowHeight = 80
DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: {
self.data.append(["day": "Mon",
"record": [
["name": "Abhya"], ["name": "Anivesh"]
]
])
self.data.append(["day": "Tue",
"record": [
["name": "Vivek"], ["name": "Arun"]
]
])
self.data.append(["day": "Wed",
"record": [
["name": "Bindu"], ["name": "Aliya"]
]
])
self.data.append(["day": "Thi",
"record": [
["name": "Vivek"], ["name": "Arun"]
]
])
print(self.data[0])
self.tableView.stopSkeletonAnimation()
self.view.hideSkeleton()
self.tableView.reloadData()
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.isSkeletonable = true
tableView.showAnimatedGradientSkeleton()
tableView.showAnimatedSkeleton(usingColor: .concrete, transition: .crossDissolve(0.25))
}
func getArray(withDictionary array: Any?) -> [Dictionary<String, Any>] {
guard let arr = array as? [Dictionary<String, Any>] else {
return []
}
return arr
}
}
extension ViewController:UITableViewDelegate {
}
extension ViewController: SkeletonTableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
self.data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let dictionary = self.data[section]
let array = getArray(withDictionary: dictionary["record"])
return array.count
}
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
return "TeacherCell"
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderTableViewCell
print("section", section)
let dict = self.data[section]
cell.dayName.text = dict["day"] as! String
return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TeacherCell") as! TeacherTableViewCell
let dictionary = self.data[indexPath.row]
let array = getArray(withDictionary: dictionary["record"])
let dict = array[indexPath.row]
cell.teacherName.text = dictionary["name"] as! String
return cell
}
}