此图是我卡顿的列表页截屏,为了避免打广告和泄漏隐私的缘故,部分地方会打马赛克,望谅解,不过绝对不影响读者阅读:
// JCWFinancialFroductListVC.swift
// Created by wll on 2017/11/9.
// Copyright © 2017年 wll. All rights reserved.
typealias RunloopBlock = ((IndexPath) -> Bool)
private let cellID = "XXXFinancialFroductListVCCell"
class XXXFinancialFroductListVC: BaseTableViewController {
/******处理滑动卡顿******///iOS11 系统bug会特别卡
var tasksArr: [RunloopBlock] = []
var tasksIndexPathArr: [IndexPath] = []
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)
override func viewDidLoad() {
self.view.jcw_showGifLoading()
self.addRunloopObserver()
self.tableView.mj_header = WLRefreshGifHeader(refreshingBlock: {
weakSelf?.pageManager.pageNo = XXXLimitNumberStarPage
self.navigationItem.title = "理财产品"
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
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)
cell.kaDunHandler2(self.pageManager.dataArr, tableView, indexPath)
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let final = FinancialDetailVC()
let model = pageManager.dataArr[indexPath.row] as! ProjectModel
self.navigationController?.pushViewController(final, animated: true, withHideBottomBarWhenPush: true, pop: false)
extension XXXFinancialFroductListVC {
func addTask(_ indexP: IndexPath, unit: @escapingRunloopBlock) {
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)
fileprivate func addRunloopObserver() {
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){
CFRunLoopAddObserver(runLoop, observer, .commonModes);
func observerCallbackFunc() -> CFRunLoopObserverCallBack {
return {(observer, activity, context) -> Void in
if context == nil {//如果没有取到 直接返回
// 崩溃的与原因不在这里 isFromADGuide 的问题 不应该取反
let vc = unsafeBitCast(context, to: XXXFinancialFroductListVC.self)
if let unit = vc.tasksArr.first, let indexP = vc.tasksIndexPathArr.first {
while (result == false && !vc.tasksArr.isEmpty && !vc.tasksIndexPathArr.isEmpty){
_ = vc.tasksArr.remove(at: 0)
_ = vc.tasksIndexPathArr.remove(at: 0)
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 asAny).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
self.tableView.mj_footer = WLRefreshFooter(refreshingBlock: {
self.tableView.mj_footer?.endRefreshingWithNoMoreData()
self.pageManager.dataArr.append(contentsOf: ProjectModel .mj_objectArray(withKeyValuesArray: resultList))
self.tableView.reloadData()
self.view.hideHUDLoading(0)
self.tableView.mj_header?.endRefreshing()
self.tableView.mj_footer?.endRefreshing()