问题描述
我想在 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",["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
}
}
解决方法
我遇到了同样的问题并在 tableView Cell 中解决了。
在 awakeNib()
中添加:someLabel.showAnimatedSkeleton()
然后添加自定义函数:
func hideSkeleton(){
someLabel.hideSkeleton()
}
现在在您的 MainView 控制器中,在返回单元格之前的 cellForItem
中调用此函数。
希望这对你有用。