func refresh(){
//如果有上拉加载更多 正在执行,则取消它
if self.tableView.mj_footer.isRefreshing() {
self.tableView.mj_footer.endRefreshing()
}
//根据 tab name 获取帖子列表
TopicListModel.getTopicList(tab){
//自定义的Response
(response:V2ValueResponse<[TopicListModel]>) -> Void in
if response.success {
//var topicList:Array<TopicListModel>?
self.topicList = response.value
self.tableView.reloadData()
}
self.tableView.mj_header.endRefreshing()
}
}
/////////////////////////////////
//MARK: - Request
extension TopicListModel {
/**
- parameter tab: tab名
*/
class func getTopicList(
_ tab: String? = nil,
page:Int = 0,
completionHandler: @escaping (V2ValueResponse<[TopicListModel]>) -> Void
)->Void{
var params:[String:String] = [:]
if let tab = tab {
params["tab"]=tab
}
else {
params["tab"] = "all"
}
var url = V2EXURL
if params["tab"] == "all" && page > 0 {
params.removeAll()
params["p"] = "\(page)"
url = V2EXURL + "recent"
}
Alamofire.request(url,parameters: params,headers: MOBILE_CLIENT_HEADERS).responseJiHtml { (response) -> Void in
var resultArray:[TopicListModel] = []
if let jiHtml = response.result.value{
if let aRootNode = jiHtml.xPath("//body/div[@id='Wrapper']/div[@class='content']/div[@class='Box']/div[@class='cell item']"){
for aNode in aRootNode {
let topic = TopicListModel(rootNode:aNode)
resultArray.append(topic);
}
}
DispatchQueue.global().async {
}
}
//在自定义的Response里面传值
let t = V2ValueResponse<[TopicListModel]>(value:resultArray,success: response.result.isSuccess)
completionHandler(t);
}
}
/////////////////////////////////
// V2Response.swift
import UIKit
class V2Response: NSObject {
var success:Bool = false
var message:String = "No message"
init(success:Bool,message:String?) {
super.init()
self.success = success
if let message = message{
self.message = message
}
}
init(success:Bool) {
super.init()
self.success = success
}
}
class V2ValueResponse<T>: V2Response {
var value:T?
override init(success: Bool) {
super.init(success: success)
}
override init(success:Bool,message:String?) {
super.init(success:success)
if let message = message {
self.message = message
}
}
convenience init(value:T,success:Bool) {
self.init(success: success)
self.value = value
}
convenience init(value:T,success:Bool,message:String?) {
self.init(value:value,success:success)
if let message = message {
self.message = message
}
}
}
原文链接:https://www.f2er.com/swift/321766.html