ios - 如何在swift中使用多个部分的骨架加载?
问题描述
我想用 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
}
}
解决方案
我有同样的问题并在 tableView 单元中解决了它。
补充awakeNib()
:someLabel.showAnimatedSkeleton()
之后添加一个自定义函数:
func hideSkeleton(){
someLabel.hideSkeleton()
}
现在在您的 MainView 控制器中cellForItem
,在返回单元格之前调用此函数。
希望这对你有用。
推荐阅读
- machine-learning - Xgboost 目标等级是位置敏感的
- node.js - TypeError:无法读取 null 的属性“数据”
- cmake - 如何从 cmake find_package 中检索包含目录?
- python - 在 Pandas 中添加一列
- amazon-redshift - 来自客户客户端计算机的 Redshift 端点名称解析失败
- ios - 如何从 textFieldDidEndEditing 访问自定义 UITableViewCell?
- amazon-web-services - AWS:需要指导来部署我的 Django 项目
- xml - Vbscript / UFT如何获取标签元素名称以验证标签元素的Xpath
- solr - 查询 solr 的计数:当 rows = 0 时省略字段?
- ruby-on-rails - 捆绑安装不起作用,显示致命错误