本文来源CocoaChina网友wanglelewang投稿
此图是我卡顿的列表页截屏,为了避免打广告和泄漏隐私的缘故,部分地方会打马赛克,望谅解,不过绝对不影响读者阅读:
//
// JCWFinancialFroductListVC.swift
// HengYouCai
//
// Created by wll on 2017/11/9.
// Copyright © 2017年 wll. All rights reserved.
// 理财产品列表--代码
import UIKit
///定义blcok
typealias RunloopBlock = ((IndexPath) -> Bool)
private let cellID = "XXXFinancialFroductListVCCell"
//MARK:界面初始化
class XXXFinancialFroductListVC: BaseTableViewController {
/******处理滑动卡顿******///iOS11 系统bug会特别卡
///时钟事件
var timer: Timer?
///任务数组
var tasksArr: [RunloopBlock] = []
///任务对应indexPath数组
var tasksIndexPathArr: [IndexPath] = []
///最大任务 10
let maxQueueLength = XXXLimitNumberPageSize
/******处理滑动卡顿******/
///上拉下拉刷新管理器
let pageManager = XXXRefreshPageManager()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//每次进来刷新
if pageManager.pageNo>1 {
let index = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: index, at: .top, animated: true)
}
pageManager.pageNo = 1
loadData()
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.jcw_showGifLoading()
///界面配置
self.createNav()
self.createCV()
/******处理滑动卡顿******/
//添加runloop的监听
self.addRunloopObserver()
/******处理滑动卡顿******/
weak var weakSelf = self
self.tableView.mj_header = WLRefreshGifHeader(refreshingBlock: {
weakSelf?.pageManager.pageNo = XXXLimitNumberStarPage
weakSelf?.loadData()
})
}
func createNav() {
self.navigationItem.title = "理财产品"
}
func createCV() {
self.tableView.register(XXXFinancialProductCell.self, forCellReuseIdentifier: cellID)
self.tableView.backgroundColor = Normal_458Color
self.tableView.separatorStyle = .none
self.tableView.estimatedRowHeight = 0
self.tableView.estimatedSectionFooterHeight = 0
self.tableView.estimatedSectionHeaderHeight = 0
///顶部间隙
let topGrayView = UIView(frame: CGRect(x: 0, y: 0, width: XXXSCREENW, height: 8))
topGrayView.backgroundColor = self.tableView.backgroundColor
self.tableView.tableHeaderView = topGrayView
}
}
//MARK:表代理
extension XXXFinancialFroductListVC {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.pageManager.dataArr.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: XXXFinancialProductCell = tableView.dequeueReusableCell(withIdentifier: cellID) as! XXXFinancialProductCell
self.addTask(indexPath) { (indexP) -> Bool in
cell.kaDunHandler(self.pageManager.dataArr, tableView, indexP)
return true
}
cell.kaDunHandler2(self.pageManager.dataArr, tableView, indexPath)
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 128
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let final = FinancialDetailVC()
let model = pageManager.dataArr[indexPath.row] as! ProjectModel
final.homeModel = model
final.idStr = model.id
self.navigationController?.pushViewController(final, animated: true, withHideBottomBarWhenPush: true, pop: false)
}
}
//MARK:处理卡顿
extension XXXFinancialFroductListVC {
///添加新的任务的方法!
func addTask(_ indexP: IndexPath, unit: @escaping RunloopBlock) {
self.tasksArr.append(unit)
self.tasksIndexPathArr.append(indexP)
//判断一下 保证没有来得及显示的cell不会绘制
if self.tasksArr.count > self.maxQueueLength {
_ = self.tasksArr.remove(at: 0)
_ = self.tasksIndexPathArr.remove(at: 0)
}
}
///添加runloop的监听
fileprivate func addRunloopObserver() {
//获取当前RunLoop
let runLoop: CFRunLoop = CFRunLoopGetCurrent()
//定义一个上下文
var context: CFRunLoopObserverContext = CFRunLoopObserverContext(version: 0, info: unsafeBitCast(self, to: UnsafeMutableRawPointer.self), retain: nil, release: nil, copyDescription: nil)
//定义一个观察者
if let observer = CFRunLoopObserverCreate(kCFAllocatorDefault, CFRunLoopActivity.beforeWaiting.rawValue, true, 0, self.observerCallbackFunc(), &context){
//添加当前RunLoop的观察者
CFRunLoopAddObserver(runLoop, observer, .commonModes);
}
}
///回调函数
func observerCallbackFunc() -> CFRunLoopObserverCallBack {
return {(observer, activity, context) -> Void in
if context == nil {//如果没有取到 直接返回
return
}
// 崩溃的与原因不在这里 isFromADGuide 的问题 不应该取反
//取出上下文 就是当前的vc
let vc = unsafeBitCast(context, to: XXXFinancialFroductListVC.self)
//取出任务
if let unit = vc.tasksArr.first, let indexP = vc.tasksIndexPathArr.first {
var result = false
while (result == false && !vc.tasksArr.isEmpty && !vc.tasksIndexPathArr.isEmpty){
//执行任务
result = unit(indexP);
//干掉第一个任务
_ = vc.tasksArr.remove(at: 0)
_ = vc.tasksIndexPathArr.remove(at: 0)
}
}
}
}
}
//MARK:列表数据请求
extension XXXFinancialFroductListVC {
fileprivate func loadData() {
XXXNetService.productsList(withDict: ["XXX":"x","pageNo":self.pageManager.pageNo,"pageSize":XXXLimitNumberPageSize], onSuccess: { (result) in
//隐藏加载框
self.view.hideHUDLoading(0)
//坑 一定记得加可选值 否则 footer可能不存在
self.tableView.mj_header?.endRefreshing()
self.tableView.mj_footer?.endRefreshing()
let resultList: [Any]? = JSON(result as Any).dictionaryValue["productsList"]?.arrayObject
if self.pageManager.pageNo == XXXLimitNumberStarPage {
self.pageManager.dataArr.removeAll()
//下拉清除任务
self.tasksArr.removeAll()
self.tasksIndexPathArr.removeAll()
}
if let count = resultList?.count, count >= XXXLimitNumberPageSize{
self.pageManager.pageNo += XXXLimitNumberStarPage
weak var weakSelf = self
self.tableView.mj_footer = WLRefreshFooter(refreshingBlock: {
weakSelf?.loadData()
})
}else{
self.tableView.mj_footer?.endRefreshingWithNoMoreData()
}
self.pageManager.dataArr.append(contentsOf: ProjectModel .mj_objectArray(withKeyValuesArray: resultList))
self.tableView.reloadData()
}) { (code, errMsg) in
//隐藏加载框
self.view.hideHUDLoading(0)
self.tableView.mj_header?.endRefreshing()
self.tableView.mj_footer?.endRefreshing()
}
}
}
好了以上就是代码,欢迎小伙伴们指点和提出疑问,我的邮箱lameshuaia@163.com