//调用的方法
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
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。