MicroCourseViewController.swift
14.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// MicroCourseViewController.swift
// YouerLiveVideo
//
// Created by 左丞 on 2017/5/15.
// Copyright © 2017年 左丞. All rights reserved.
//
import UIKit
import AVKit
class MicroCourseViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate{
@IBOutlet var tableView: UITableView!
@IBOutlet var grayView: UIView!
@IBOutlet var searchTableView: UITableView!
@IBOutlet var searchTableHeight: NSLayoutConstraint!
var searchController:UISearchController!
var dataSetAry:[String]=["葛建军","张道峰","徐晓杰","左承","段合江"]
var searchAry:[MicroCourseModel]=[]
var currentPage:Int=1 //当前页数,每次取十个数据
var resourceData:[MicroCourseModel]=[]
var parameter:Dictionary<String,AnyObject>?
override func viewDidLoad() {
super.viewDidLoad()
self.configTheme()
addSearchController()
addRefresh()
self.refreshLivesAction(parameters: ["type":0 as AnyObject,"orderby":1 as AnyObject,"pageIndex":self.currentPage as AnyObject,"pageSize":10 as AnyObject], completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
})
NotificationCenter.default.addObserver(self, selector: #selector(ResourceViewController.reloadTableView), name: NSNotification.Name(rawValue: "FilterReloadTableView"), object: nil)
}
//筛选结束后刷新界面
func reloadTableView(noti:Notification){
let parameters=(noti.object as! Dictionary<String,AnyObject>)
if parameters["type"]?.int8Value==0{
parameter=parameters
currentPage=1
resourceData.removeAll()
refreshLivesAction(parameters: parameters, completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
})
}
}
func addSearchController(){
self.definesPresentationContext=true
searchController=UISearchController(searchResultsController: nil)
searchController.delegate=self
searchController.searchBar.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=1
self.resourceData.removeAll()
var p:Dictionary<String,AnyObject>=["type":0 as AnyObject,"orderby":1 as AnyObject,"pageIndex":self.currentPage as AnyObject,"pageSize":10 as AnyObject]
if self.parameter != nil {
self.parameter!.updateValue(self.currentPage as AnyObject, forKey: "pageIndex")
p=self.parameter!
}
self.refreshLivesAction(parameters: p, 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: {
//上拉加载更多
var p:Dictionary<String,AnyObject>=["type":0 as AnyObject,"orderby":1 as AnyObject,"pageIndex":self.currentPage as AnyObject,"pageSize":10 as AnyObject]
if self.parameter != nil {
self.parameter!.updateValue(self.currentPage as AnyObject, forKey: "pageIndex")
p=self.parameter!
}
self.refreshLivesAction(parameters: p, completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
self.tableView.mj_footer.endRefreshing()
})
})
}
// MARK: - 刷新数据接口
func refreshLivesAction(isSearch:Bool=false,parameters:Dictionary<String,AnyObject>,completionHandler:@escaping (Bool) -> ()){
AppDelegate.instance().httpServer.postResourceList(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 isSearch {
for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
self.searchAry.append(MicroCourseModel(j: item))
}
}else{
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
})
}else{
success=false
}
}else{
success=false
}
completionHandler(success)
}
}
// MARK: - 跳转到筛选
@IBAction func jumpFilterVCAction(_ sender: UIButton) {
let vc = UIStoryboard(name: "Resource", bundle: nil).instantiateViewController(withIdentifier: "FilterViewController") as! FilterViewController
vc.type=FilterParameter(type: 0)
self.present(vc.wrapWithNavigationController(), animated: true, completion: nil)
}
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].f_Title
return cell!
}else{
let cell=tableView.dequeueReusableCell(withIdentifier: "identifier") as! MicroCourseViewControllerTableViewCell
cell.superVC=self
cell.setUpWith(item: resourceData[indexPath.row])
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// let vc = UIStoryboard(name: "Resource", bundle: nil).instantiateViewController(withIdentifier: "LiveInforViewControllerVC") as! LiveInforViewController
// vc.item=item
// self.superVC.navigationController!.pushViewController(vc, animated: true)
var item:MicroCourseModel!
if tableView==searchTableView {
item=searchAry[indexPath.row]
}else{
item=resourceData[indexPath.row]
}
let url = URL(string:item.f_ResourceUrl)
let player = AVPlayerViewController()
player.player = AVPlayer(url: url!)
player.videoGravity = AVLayerVideoGravityResizeAspect
self.present(player, animated: true, completion: nil)
AppDelegate.instance().httpServer.updatePv(parameters: ["f_id":item.f_Id as AnyObject]) { (str, error) in
if error==nil {
if JSON.fromString(jsonString: str)!["status"].intValue == 1{
//更新阅读量成功刷新页面
self.currentPage=1
self.resourceData.removeAll()
var p:Dictionary<String,AnyObject>=["type":0 as AnyObject,"orderby":1 as AnyObject,"pageIndex":self.currentPage as AnyObject,"pageSize":10 as AnyObject]
if self.parameter != nil {
self.parameter!.updateValue(self.currentPage as AnyObject, forKey: "pageIndex")
p=self.parameter!
}
self.tableView.reloadData()
self.refreshLivesAction(parameters: p, completionHandler: { (success) in
if success{
self.tableView.reloadData()
}
})
}
}
}
AppDelegate.instance().httpServer.addHistoryView(parameters: ["f_ViewType":0 as AnyObject,
"f_ViewId":item.f_Id as AnyObject,
"f_ViewName":item.f_Title as AnyObject]) { (str, error) in
}
}
//取消键盘输入状态
@IBAction func cancleSearchActivit(_ sender: UITapGestureRecognizer) {
self.searchAry.removeAll()
searchTableHeight.constant=44
self.searchTableView.reloadData()
searchController.isActive=false
}
func willPresentSearchController(_ searchController: UISearchController) {
grayView.isHidden=false
}
func willDismissSearchController(_ searchController: UISearchController) {
searchTableHeight.constant=44
grayView.isHidden=true
}
//searchaBarDelegate
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
refreshLivesAction(isSearch: true,parameters: ["type":0 as AnyObject,"orderby":1 as AnyObject,"pageIndex":1 as AnyObject,"keyword":searchBar.text as AnyObject,"pageSize":10 as AnyObject], completionHandler: { (success) in
if success{
if CGFloat((self.searchAry.count+1)*44)>(getScreenHeight()-64){
self.searchTableHeight.constant=getScreenHeight()-64
}else{
self.searchTableHeight.constant=CGFloat((self.searchAry.count+1)*44)
}
self.searchTableView.reloadData()
}
})
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.searchAry.removeAll()
searchTableHeight.constant=44
self.searchTableView.reloadData()
searchController.isActive=false
}
//谓词搜索过滤
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()
}
}
class MicroCourseViewControllerTableViewCell:UITableViewCell{
@IBOutlet var iconImageView: UIImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var updateTeacherLabel: UILabel!
@IBOutlet var timeLabel: UILabel!
@IBOutlet var downLoadCountLabel: UILabel!
var item:MicroCourseModel!
var superVC:MicroCourseViewController!
func setUpWith(item: MicroCourseModel){
self.item=item
iconImageView.sd_setImage(with: URL(string: item.f_Img), placeholderImage: UIImage(named: "microCourse_default"))
titleLabel.text=item.f_Title
updateTeacherLabel.text="主讲师:\(item.f_CreatorName)"
timeLabel.text=setDateToString(date:dateFromISO8601(dateString: item.f_CreatorTime))
downLoadCountLabel.text="\(item.f_Pv)"
}
@IBAction func pushDetailVC(_ sender: UIButton) {
// let vc = UIStoryboard(name: "Resource", bundle: nil).instantiateViewController(withIdentifier: "LiveInforViewControllerVC") as! LiveInforViewController
// vc.item=item
// self.superVC.navigationController!.pushViewController(vc, animated: true)
let url = URL(string:item.f_ResourceUrl)
let player = AVPlayerViewController()
player.player = AVPlayer(url: url!)
player.videoGravity = AVLayerVideoGravityResizeAspect
self.superVC.present(player, animated: true, completion: nil)
AppDelegate.instance().httpServer.updatePv(parameters: ["f_id":item.f_Id as AnyObject]) { (str, error) in
if error==nil {
if JSON.fromString(jsonString: str)!["status"].intValue == 1{
//更新阅读量成功刷新页面
self.superVC.currentPage=1
self.superVC.resourceData.removeAll()
var p:Dictionary<String,AnyObject>=["type":0 as AnyObject,"orderby":1 as AnyObject,"pageIndex":self.superVC.currentPage as AnyObject,"pageSize":10 as AnyObject]
if self.superVC.parameter != nil {
self.superVC.parameter!.updateValue(self.superVC.currentPage as AnyObject, forKey: "pageIndex")
p=self.superVC.parameter!
}
self.superVC.refreshLivesAction(parameters: p, completionHandler: { (success) in
if success{
self.superVC.tableView.reloadData()
}
})
}
}
}
AppDelegate.instance().httpServer.addHistoryView(parameters: ["f_ViewType":0 as AnyObject,
"f_ViewId":item.f_Id as AnyObject,
"f_ViewName":item.f_Title as AnyObject]) { (str, error) in
}
}
}