ResourceViewController.swift
8.57 KB
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
192
193
194
195
196
197
198
199
200
//
// ResourceViewController.swift
// YouerLiveVideo
//
// Created by 左丞 on 2017/5/15.
// Copyright © 2017年 左丞. All rights reserved.
//
import UIKit
class ResourceViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating {
@IBOutlet var tableView: UITableView!
@IBOutlet var grayView: UIView!
@IBOutlet var searchTableView: UITableView!
@IBOutlet var searchTableHeight: NSLayoutConstraint!
var searchController:UISearchController!
var dataSetAry:[String]=["葛建军","张道峰","徐晓杰","左承","段合江"]
var searchAry:[String]=[]
var currentPage:Int=0 //当前页数,每次取十个数据
var resourceData:[MicroCourseModel]=[]
override func viewDidLoad() {
super.viewDidLoad()
self.configTheme()
addSearchController()
addRefresh()
self.refreshLivesAction(tag: self.currentPage, completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
})
}
func addSearchController(){
self.definesPresentationContext=true
searchController=UISearchController(searchResultsController: nil)
searchController.delegate=self
searchController.searchResultsUpdater=self
searchController.searchBar.barTintColor=UIColor.groupTableViewBackground
searchController.searchBar.placeholder="搜索"
//设置UISearchController的显示属性,以下3个属性默认为YES
//搜索时,背景变暗色
self.searchController.dimsBackgroundDuringPresentation=false
//搜索时,背景变模糊
// self.searchController.obscuresBackgroundDuringPresentation=false
//点击搜索的时候,是否隐藏导航栏
// searchController.hidesNavigationBarDuringPresentation=true
//位置
searchController.searchBar.frame=CGRect(x: self.searchController.searchBar.frame.origin.x, y: self.searchController.searchBar.frame.origin.y, width: self.searchController.searchBar.frame.size.width, height: 44.0);
// self.view.addSubview(searchController.searchBar)
searchTableView.tableHeaderView=searchController.searchBar
}
func addRefresh(){
let header=MJRefreshNormalHeader(refreshingBlock: {
//下拉刷新
self.currentPage=0
self.resourceData.removeAll()
self.refreshLivesAction(tag: self.currentPage, completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
self.tableView.mj_header.endRefreshing()
})
})
header?.lastUpdatedTimeLabel.isHidden=true
tableView.mj_header=header
tableView.mj_footer=MJRefreshBackNormalFooter(refreshingBlock: {
//上拉加载更多
self.refreshLivesAction(tag: self.currentPage, completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
self.tableView.mj_footer.endRefreshing()
})
})
}
// MARK: - 刷新数据接口
func refreshLivesAction(tag:Int,completionHandler:@escaping (Bool) -> ()){
let parameters:Dictionary<String,AnyObject>=["type":1 as AnyObject,"pageIndex":tag as AnyObject,"pageSize":1 as AnyObject]
AppDelegate.instance().httpServer.getHotRescourse(parameters: parameters) { (str, error) in
var success:Bool=true
if error==nil {
if JSON.fromString(jsonString: str)!["status"].intValue == 1{
httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
if JSON.fromString(jsonString: str)!["data"].arrayValue.count>0{
self.currentPage+=1
}
for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
self.resourceData.append(MicroCourseModel(j: item))
}
success=true
}, failHandler: { (error) in
success=false
appRootViewController().view.makeToast("获取资源失败:\(error.localizedDescription)")
})
}else{
success=false
appRootViewController().view.makeToast("获取资源失败:\(JSON.fromString(jsonString: str)!["message"].stringValue)")
}
}else{
success=false
appRootViewController().view.makeToast("获取资源失败:\(error!.description)")
}
completionHandler(success)
}
}
// MARK: - 跳转到筛选
@IBAction func jumpFilterVCAction(_ sender: UIButton) {
let vc = UIStoryboard(name: "Resource", bundle: nil).instantiateViewController(withIdentifier: "FilterViewController") as! FilterViewController
self.navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView==searchTableView{
return searchAry.count
}else{
return resourceData.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView==searchTableView {
let cell=tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text=searchAry[indexPath.row]
return cell!
}else{
let cell=tableView.dequeueReusableCell(withIdentifier: "identifier") as! ResourceViewControllerTableViewCell
cell.setUpWith(item: resourceData[indexPath.row])
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView==searchTableView {
let vc = UIStoryboard(name: "HomePage", bundle: nil).instantiateViewController(withIdentifier: "RankingTableViewController") as! RankingTableViewController
self.navigationController?.pushViewController(vc, animated: true)
}else{
}
}
//取消键盘输入状态
@IBAction func cancleSearchActivit(_ sender: UITapGestureRecognizer) {
searchController.isActive=false
}
func willPresentSearchController(_ searchController: UISearchController) {
grayView.isHidden=false
}
func willDismissSearchController(_ searchController: UISearchController) {
searchTableHeight.constant=44
grayView.isHidden=true
}
//谓词搜索过滤
func updateSearchResults(for searchController: UISearchController) {
let searchString=searchController.searchBar.text
let predicate=NSPredicate(format: "SELF CONTAINS[c] %@", searchString!)
searchAry=(dataSetAry as NSArray).filtered(using: predicate) as! [String]
if CGFloat((searchAry.count+1)*44)>(getScreenHeight()-64){
searchTableHeight.constant=getScreenHeight()-64
}else{
searchTableHeight.constant=CGFloat((searchAry.count+1)*44)
}
searchTableView.reloadData()
}
}
extension ResourceViewController:UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ResourceViewControllerCollectionCell
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
}
class ResourceViewControllerTableViewCell:UITableViewCell{
@IBOutlet var iconImageView: UIImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var updateTeacherLabel: UILabel!
@IBOutlet var timeLabel: UILabel!
@IBOutlet var downLoadCountLabel: UILabel!
func setUpWith(item: MicroCourseModel){
iconImageView.sd_setImage(with: URL(string: item.f_Img), placeholderImage: UIImage(named: "icon_course_placeholder"))
titleLabel.text=item.f_Title
updateTeacherLabel.text="上传者:\(item.f_CreatorName) 老师"
timeLabel.text=item.f_CreatorTime
downLoadCountLabel.text="\(item.f_DownloadSum)"
}
}
class ResourceViewControllerCollectionCell:UICollectionViewCell{
}