用Swift5写的比较完整的tableview,包括上滑继续加载,下拉刷新
class HKPovertyFamilyController: UITableViewController {
//表格底部用来提示数据加载的视图
var loadMoreView:UIView?
//用了记录当前是否允许加载新数据(正则加载的时候会将其设为false,放置重复加载)
var loadMoreEnable = true
//下拉刷新控件
let refresh = UIRefreshControl()
//存放最后一条数据的ID
let newDataDefaults = UserDefaults.standard
lazy var familyViewModel = HKPovertyFamilyViewModel()
override func viewDidLoad() {
super.viewDidLoad()
setRefresh()
tableView.rowHeight = 100
loadFamilyData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return familyViewModel.povertyModelList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let poverty = familyViewModel.povertyModelList[indexPath.row]
let cell: HKPovertyFamilyCell = tableView.dequeueReusableCell(withIdentifier: "povertyFamily") as! HKPovertyFamilyCell
cell.householderLabel.text = "户主:"+poverty.name
cell.populationLabel.text = "人口:"+poverty.family
cell.helpTimesLabel.text = "帮扶次数:"+poverty.num + "次"
if (poverty.bfzrr == "") {
cell.helpResponsibleLabel.text = "帮扶责任人:暂无"
} else if (poverty.bfzrr == nil){
cell.helpResponsibleLabel.text = "帮扶责任人:"+(poverty.bfzrr ?? "暂无")
}
if (poverty.time == "") {
cell.recentHelpLabel.text = "最近帮扶:暂无"
} else if (poverty.time == nil) {
cell.recentHelpLabel.text = "最近帮扶:"+(poverty.time ?? "暂无")
}
//当下拉到底部,执行loadMore()
if (loadMoreEnable && indexPath.row == familyViewModel.povertyModelList.count - 1) {
loadMore()
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let story = UIStoryboard.init(name: "HKPovertyFamily", bundle: nil)
let detailVC = story.instantiateViewController(withIdentifier: "povertyFamilyDetail")
let poverty = familyViewModel.povertyModelList[indexPath.row]
let useDefaults = UserDefaults.standard
useDefaults.set(poverty.id, forKey: "poor_id")
useDefaults.synchronize()
// print("--------id\(poverty.id)")
self.navigationController?.pushViewController(detailVC, animated: true)
}
//上拉刷新视图
private func setupInfiniteScrollingView() {
self.loadMoreView = UIView(frame: CGRect(x:0, y:self.tableView.contentSize.height,
width:self.tableView.bounds.size.width, height:60))
self.loadMoreView!.autoresizingMask = UIView.AutoresizingMask.flexibleWidth
self.loadMoreView!.backgroundColor = UIColor.orange
//添加中间的环形进度条
let activityViewIndicator = UIActivityIndicatorView(style: .white)
activityViewIndicator.color = UIColor.darkGray
let indicatorX = self.view.frame.width/2-activityViewIndicator.frame.width/2
let indicatorY = self.loadMoreView!.frame.size.height/2-activityViewIndicator.frame.height/2
activityViewIndicator.frame = CGRect(x:indicatorX, y:indicatorY,
width:activityViewIndicator.frame.width,
height:activityViewIndicator.frame.height)
activityViewIndicator.startAnimating()
self.loadMoreView!.addSubview(activityViewIndicator)
}
}
extension HKPovertyFamilyController {
//加载数据
func loadFamilyData() -> Void {
familyViewModel.returnData {
//将最后一条信息存入defaults中,为了下拉刷新判断是否是新数据
let newElement = self.familyViewModel.povertyModelList.last!
self.newDataDefaults.setValue(newElement.id, forKey: "newId")
self.tableView.reloadData()
}
}
//加载更多数据(上拉加载的数据)
func loadMore(){
print("加载新数据!")
loadMoreEnable = false
self.familyViewModel.returnData {
self.familyViewModel.startAt += 10
self.tableView.reloadData()
}
loadMoreEnable = true
}
//下拉刷新方法
@objc func loadNewData() -> Void {
//取出ID和后面作比较
let newElement = self.newDataDefaults.string(forKey: "newId")
familyViewModel.returnData {
if newElement != self.familyViewModel.povertyModelList.last?.id {
let newMessage = self.familyViewModel.povertyModelList.last
self.familyViewModel.povertyModelList.insert(newMessage!, at: 0)
self.tableView.reloadData()
}
}
self.refresh.endRefreshing()
}
}
extension HKPovertyFamilyController {
//设置刷新控件
func setRefresh() -> Void {
refresh.tintColor = .red
refresh.attributedTitle = NSAttributedString(string: "正在刷新")
refresh.addTarget(self, action: #selector(loadNewData), for: .valueChanged)
self.tableView.addSubview(refresh)
}
}
网友评论