When I did the swift version of the waterfall stream demo "Swift UITableView Waterfall stream/nsurlconnection Asynchronous network Request", the use of the Nsurlconnection made the network asynchronous request, the image of the asynchronous loading using GCD do. In the use of the process, the network request part is not a problem, but in the image of the asynchronous loading, because the picture is not cached, so when sliding up and down, you need to constantly load the picture, so the user experience is not good.
In OC, we have afnetworking and sdwebimage do the loading of the network and the loading of the picture. Then there are similar in swift: Alamofire and Kingfisher. The former is the network load, the latter is the picture loading. The use of cocoapods is integrated into the process. The integration of Cocoapods can be used to explore the use of IOS development cocoapods.
So in this small Swift project, a total of four third-party libraries were integrated: Alamofire/kingfisher/mjrefresh/mbprogresshud.
About the integration of Alamofire/kingfisher/mjrefresh, according to this three third library above the Cocoapods method integration can be, you can look at the contents of the Podfile file:
Source ' Https://github.com/CocoaPods/Specs.git 'Platform:iOS,' 9.0 'Use_frameworks!pod ' Alamofire ',' ~> 3.3 'Source ' Https://github.com/CocoaPods/Specs.git 'Platform:iOS,' 8.0 'Use_frameworks!pod ' Kingfisher ',' ~> 2.4 'pod ' Mjrefresh 'Use_frameworks!
But the integrated MBPROGRESSHUD requires manual drag-and-drop integration. After we dragged the Mbprogresshud. m and. h files in, we were prompted to create a new swift and OC bridging file to see Swift Swift and OC
#import "MBProgressHUD.h"
You can use Mbprogresshud when you put it in a bridge file.
For the four third-party libraries above, you can view GitHub or Baidu.
Let's look at this project first:
The API used is the SHOWAPI above.
Initialize the view:
Func Initview () { Self. TableView. registernib(Uinib (nibname:"Wxtableviewcell", Bundle:Nil), Forcellreuseidentifier:"Wxtableviewcell")//Add drop-down refresh Self. TableView. Mj_header= Mjrefreshnormalheader (refreshingtarget: Self, Refreshingaction:#selector (Viewcontroller.headrefresh)) //Add pull-up load more Self. TableView. Mj_footer= Mjrefreshautonormalfooter (refreshingtarget: Self, Refreshingaction:#selector (viewcontroller.addmoredata))}
The view is used by the IB pull.
Alamofire Network Request section:
//MARK: Network Request /** Network Request-Parameter pageIndex: pages */Func RequestData (pageindex:string) { Self. Showhud ()//Set Request parametersLet Showapi_timestamp = Self. GETDATASTR () Let parameters = ["Key":"","Showapi_appid": Showapi_appid,"Showapi_sign": Showapi_sign,"Page": PageIndex,"Showapi_timestamp": Showapi_timestamp,] alamofire.request (. GET, BASEURL, parameters:parameters). Responsejson {response inSwitchresponse.result{ Case. Success (Let Dice): Self. Hidehud () Let Dice1 = dice["Showapi_res_body"] as! Nsdictionary Let Dice2 = dice1["Pagebean"] as! Nsdictionary Self. Allpages = dice2["Allpages"] as! NSNumber Let ContentList = dice2["ContentList"] as! Nsarray//drop-down refresh array empty ifPageIndex = ="1"{ Self. Dataarray.removeallobjects ()} forDatadice in contentlist{Let model = Datamodel ()//data to model added into array Self. Dataarray.addobject (Model.makedatamodel (datadice as! nsdictionary))} Self. Flag =2 Self. Tableview.reloaddata ()//Refresh complete up/down Self. TableView.mj_header.endRefreshing () Self. TableView.mj_footer.endRefreshing () Case. Failure (let error):Print(Error) Self. Hidehud ()}}}
Get current time:
/** 获取当前时间 - returns: 当前时间 */ func getDataStr() -> String{ let date1 = NSDate() let dataFormat = NSDateFormatter.init() // yyyyMMddHHmmss "yyyyMMddHHmmss" let dataString = dataFormat.stringFromDate(date1) as String return dataString }
To implement a pull-down method:
// MARK: 上下拉 /** 下拉刷新 */ func headRefresh() { self.requestData("1") } /** 上拉加载更多 */ func addMoreData() { 1 if self.currentPage < self.allPages.integerValue { let page = String(self.currentPage) self .requestData(page) } }
Use of Mbprogresshud:
// MARK: 菊花 func showHUD() { let hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true) hud.mode = MBProgressHUDMode.Indeterminate hud.labelText"数据加载中……" hud.dimBackground = true } func hideHUD() { MBProgressHUD.hideAllHUDsForView(self.view, animated: true) }
Use of Kingfisher
The use of Kingfisher is relatively simple to use. It is primarily used to populate the cell with data.
/** populating cell with data-parameter DataArray: Data collection-Parameter Indexpath:indexpath *Func Fillcellwiftdataarray (Dataarray:nsarray, Indexpath:nsindexpath) {Let model = Dataarray[indexpath. Row] As! Datamodel Self. Userimageview. KF_setimagewithurl (Nsurl (String:model. Userlogo)!) Self. Usernamelabel. Text= Model. UserNameSelf. Timerlabel. Text= Model. Date;Self. Mainimageview. KF_setimagewithurl (Nsurl (String:model. Contentimg)!) Self. Contentlabel. Text= Model. TitleSelf. Typelabel. Text= Model. TypeName}
Basic four third-party libraries
This is basically the simple use of alamofire/kingfisher/mjrefresh/mbprogresshud.
Summarize
In this small project, it looks like there are only two pages, but we learned how to integrate alamofire/kingfisher/mjrefresh/mbprogresshud and use, cocoapods use, swift and OC integration.
But there is still need to improve, such as can be mbprogresshud again, so it is more convenient to use; You can create a new. pch file, place a header file in a. pch file that needs to be used in multiple places, so that you do not need to write the header file more than once in the process you are using, and so on.
Code: Alamofire-kingfisher
Small project of Swift integrated Alamofire/kingfisher/mjrefresh/mbprogresshud