您好,登錄后才能下訂單哦!
//調用的方法
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("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里面傳值
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。