您好,登錄后才能下訂單哦!
//調(diào)用的方法
func refresh(){
//如果有上拉加載更多 正在執(zhí)行,則取消它
if self.tableView.mj_footer.isRefreshing() {
self.tableView.mj_footer.endRefreshing()
}
//根據(jù) 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("http://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里面?zhèn)髦?/span>
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
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。