-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathArticleCollectViewController.swift
191 lines (144 loc) · 6.83 KB
/
ArticleCollectViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// ArticleCollectViewController.swift
// itjh_ios
//
// Created by LijunSong on 15/3/29.
// Copyright (c) 2015年 LijunSong. All rights reserved.
//
import UIKit
import Alamofire
import SCLAlertView
import SwiftyJSON
class ArticleCollectViewController: BaseViewController {
@IBOutlet weak var atableView: UITableView!
var url = userCollecList
var userId:String = ""
var currentArticleData:[Article] = []
override func viewDidLoad() {
super.viewDidLoad()
self.atableView.tableFooterView=UIView()
//加载nib
var nib = UINib(nibName: "HomeArticleTableViewCell", bundle: nil)
self.atableView.registerNib(nib, forCellReuseIdentifier: identifier)
self.navigationTitle.text = "我的收藏"
self.atableView.addLegendHeaderWithRefreshingBlock { () -> Void in
self.loadNewData()
}
self.atableView.legendHeader.beginRefreshing()
self.atableView.addLegendFooterWithRefreshingBlock { () -> Void in
self.loadMoreData()
}
self.atableView.footer.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: 加载数据
func loadData(offset:Int, size:Int,from:String){
//接口url
var articleUrl = url + userId + "/\(offset)/\(size)"
// 请求数据
Alamofire.request(.GET, articleUrl).responseJSON { (_, response, JSON_DATA, error) -> Void in
if JSON_DATA == nil{
SCLAlertView().showWarning("温馨提示", subTitle:"您的网络在开小差,赶紧制服它,精彩的文章在等你.", closeButtonTitle:"去制服")
return
}else{
if self.PAGE_NUM == 0{
if !self.currentArticleData.isEmpty{
self.currentArticleData.removeAll(keepCapacity: false)
}
}
let data = JSON(JSON_DATA!)
let articlesArray = data["content"].arrayValue
if articlesArray.count > 0{
for currentArticle in articlesArray{
let article = Article()
article.aid = currentArticle["aid"].int!
article.title = currentArticle["title"].string!
article.date = currentArticle["date"].string!
article.img = currentArticle["img"].string!
article.author_id = currentArticle["author_id"].int!
article.author = currentArticle["author"].string!
self.currentArticleData.append(article)
}
//刷新tableview
self.atableView.reloadData()
if from == "header" {
// 拿到当前的下拉刷新控件,结束刷新状态
self.atableView.header.endRefreshing()
self.atableView.footer.hidden = false
}else{
self.atableView.footer.endRefreshing();
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
//end
}else{
self.atableView.footer.noticeNoMoreData()
}
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentArticleData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> HomeArticleTableViewCell {
var cell:HomeArticleTableViewCell! = atableView.dequeueReusableCellWithIdentifier(identifier) as! HomeArticleTableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.atitle.text = currentArticleData[indexPath.row].title as String
cell.aimg.sd_setImageWithURL(NSURL(string: currentArticleData[indexPath.row].img as String), placeholderImage: UIImage(named: "default_showPic.png"))
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var data = self.currentArticleData[indexPath.row]
var detailCtrl = ArticlesShowViewController(nibName: "ArticlesShowViewController", bundle: nil);
detailCtrl.artID = data.aid
detailCtrl.atitle = data.title as String
detailCtrl.aimg = data.img as String
detailCtrl.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(detailCtrl, animated: true)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 95
}
func randomInRange(range: Range<Int>) -> Int {
let count = UInt32(range.endIndex - range.startIndex)
return Int(arc4random_uniform(count)) + range.startIndex
}
// MARK: 上拉加载数据
func loadMoreData(){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
// 1.添加数据
self.PAGE_NUM += 1
loadData(self.PAGE_NUM, size: SHOW_NUM,from: "footer")
// 2.刷新表格
// 拿到当前的上拉刷新控件,结束刷新状态
/*let delayInSeconds:Int64 = 1000000000 * 2
var popTime:dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW,delayInSeconds)
dispatch_after(popTime, dispatch_get_main_queue(), {
self.atableView.reloadData()
self.atableView.footer.endRefreshing();
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
})*/
}
// MARK: 下拉刷新数据
func loadNewData(){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
// 1.添加假数据
self.PAGE_NUM = 0
loadData(self.PAGE_NUM, size: SHOW_NUM,from: "header")
// 2.模拟2秒后刷新表格UI(真实开发中,可以移除这段gcd代码)
/*let delayInSeconds:Int64 = 1000000000 * 1
var popTime:dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW,delayInSeconds)
dispatch_after(popTime, dispatch_get_main_queue(), {
self.atableView.reloadData()
// 拿到当前的下拉刷新控件,结束刷新状态
self.atableView.header.endRefreshing()
self.atableView.footer.hidden = false
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
});*/
}
}