去年写过一个OC版本的瀑布流Demo《【iOS】UITableView实现的瀑布流效果》。@H_502_3@
接触Swift一段时间了,今天就是用Swift再写了一个瀑布流的Demo。原理是一样的这里不再赘述。在写这个Demo的过程中是用到了NSURLConnection的异步网络请求和GCD做了一个图片的异步加载,没有做图片的缓存,所以是用起来有些卡。cell是带左滑删除视图的,删除功能是没有实现的。@H_502_3@
效果图:@H_502_3@
@H_502_3@
@H_502_3@
Demo工作流程:@H_502_3@
1.制作三个tableiVIEw@H_502_3@
2.网络请求,获取图片数据模型(Title和url等)@H_502_3@
3.数据封装成模型@H_502_3@
4.reload tableVIEw@H_502_3@
图片数据模型@H_502_3@
@H_502_3@
代码:@H_502_3@
PICModel.swift
@H_502_3@
@H_502_3@
import UIKitclass PICModel: NSObject { var album_ID : NSNumber? var brand_name : String? var created_at : NSNumber? var height : NSNumber? var ID : String? var new_height : NSNumber? var new_wIDth : NSNumber? var pic_url_d : String? var pic_url_x : String? var price : String? var Title : String? var wIDth : NSNumber?}VIEwController.swift
//// VIEwController.swift// 瀑布流-Swift//// Created by zhuming on 16/3/29.// copyright © 2016年 zhuming. All rights reserved.//import UIKitclass VIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource,UIAlertVIEwDelegate{ var tableVIEw1 : UItableVIEw? var tableVIEw2 : UItableVIEw? var tableVIEw3 : UItableVIEw? var indexPath : NSIndexPath? var menbers1 = [NSDictionary]() var menbers2 = [NSDictionary]() var menbers3 = [NSDictionary]() var PICModelArray1 = [PICModel]() var PICModelArray2 = [PICModel]() var PICModelArray3 = [PICModel]() var dataArray = ["1","2","3","4","5","6","7","8","9","10","11","12"] let urlString1 = "http://itugo.com/clIEnt/ios/API/getpicList?_version=20140117.2.5.1&_req_from=oc&_source=ios&type=&_uuID=efe47094e00109db8c28cf0ae9b607b9&max=&tag=&_promotion_channel=App%20Store&_platform=iPhone&sort=new&from=tag&_uiID=2FF998CF0D2A40E7AF6F8FAFB8F57538&_net=wifi&min=0"; let urlString2 = "http://itugo.com/clIEnt/ios/API/getpicList?_version=20140117.2.5.1&_req_from=oc&_source=ios&type=accessary&_uuID=efe47094e00109db8c28cf0ae9b607b9&max=&tag=&_promotion_channel=App%20Store&_platform=iPhone&sort=new&from=tag&_uiID=2FF998CF0D2A40E7AF6F8FAFB8F57538&_net=wifi&min=0"; let urlString3 = "http://itugo.com/clIEnt/ios/API/getpicList?_version=20140117.2.5.1&_req_from=oc&_source=ios&type=shoes&_uuID=efe47094e00109db8c28cf0ae9b607b9&max=&tag=&_promotion_channel=App%20Store&_platform=iPhone&sort=new&from=tag&_uiID=2FF998CF0D2A40E7AF6F8FAFB8F57538&_net=wifi&min=0"; overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() self.inittableVIEw() self.requestData(urlString1) self.requestData(urlString2) self.requestData(urlString3) // Do any additional setup after loading the vIEw,typically from a nib. } /** 初始化表格 */ func inittableVIEw() { self.tableVIEw1 = UItableVIEw.init(frame: CGRectMake(5,20,100,548)) self.tableVIEw1!.delegate = self self.tableVIEw1!.dataSource = self; self.tableVIEw1?.showsverticalScrollindicator = false self.tableVIEw1!.registerNib(UINib(nibname:"PBtableVIEwCell",bundle:nil),forCellReuseIDentifIEr:"PBtableVIEwCell") self.vIEw .addSubvIEw(self.tableVIEw1!) self.tableVIEw2 = UItableVIEw.init(frame: CGRectMake(110,548)) self.tableVIEw2!.delegate = self self.tableVIEw2!.dataSource = self; self.tableVIEw2?.showsverticalScrollindicator = false self.tableVIEw2!.registerNib(UINib(nibname:"PBtableVIEwCell",forCellReuseIDentifIEr:"PBtableVIEwCell") self.vIEw .addSubvIEw(self.tableVIEw2!) self.tableVIEw3 = UItableVIEw.init(frame: CGRectMake(215,548)) self.tableVIEw3!.delegate = self self.tableVIEw3!.dataSource = self; self.tableVIEw3?.showsverticalScrollindicator = false self.tableVIEw3!.registerNib(UINib(nibname:"PBtableVIEwCell",forCellReuseIDentifIEr:"PBtableVIEwCell") self.vIEw .addSubvIEw(self.tableVIEw3!) } // 网络请求数据 func requestData(urlString : String){ let url = NSURL(string: urlString) let request = NSURLRequest(URL: url!) NSURLConnection.sendAsynchronousRequest(request,queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse?,data:NSData?,error:NSError?) in let JsonResult:NSDictionary = try! NSJsONSerialization.JsONObjectWithData(data!,options: NSJsONReadingOptions.MutableContainers) as! NSDictionary let dice:NSDictionary = (JsonResult["data"] as? NSDictionary)! if (url!.isEqual(NSURL(string: self.urlString1))){ self.menbers1 = dice["data"] as! Array print("self.menbers = \(self.menbers1.count)") for value in self.menbers1{ let dice:NSDictionary = value self.PICModelArray1.append(self .makePICModel(dice)) } print("PICModelArray = \(self.PICModelArray1.count)") self.tableVIEw1?.reloadData() } if (url!.isEqual(NSURL(string: self.urlString2))){ self.menbers2 = dice["data"] as! Array print("self.menbers = \(self.menbers2.count)") for value in self.menbers2{ let dice:NSDictionary = value self.PICModelArray2.append(self .makePICModel(dice)) } print("PICModelArray = \(self.PICModelArray2.count)") self.tableVIEw2?.reloadData() } if (url!.isEqual(NSURL(string: self.urlString3))){ self.menbers3 = dice["data"] as! Array print("self.menbers = \(self.menbers3.count)") for value in self.menbers3{ let dice:NSDictionary = value self.PICModelArray3.append(self .makePICModel(dice)) } print("PICModelArray = \(self.PICModelArray3.count)") self.tableVIEw3?.reloadData() } } } // 数据封装成模型 func makePICModel(dice : NSDictionary) -> PICModel{ let model = PICModel.init() model.album_ID = dice["album_ID"] as? NSNumber model.brand_name = dice["brand_name"] as? String model.created_at = dice["created_at"] as? NSNumber model.height = dice["height"] as? NSNumber model.ID = dice["ID"] as? String model.new_height = dice["new_height"] as? NSNumber model.new_wIDth = dice["new_wIDth"] as? NSNumber model.pic_url_d = dice["pic_url_d"] as? String model.pic_url_x = dice["pic_url_x"] as? String model.price = dice["price"] as? String model.Title = dice["Title"] as? String model.wIDth = dice["wIDth"] as? NSNumber return model } // UItableVIEwDelegate,UItableVIEwDataSource func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { if tableVIEw.isEqual(self.tableVIEw1) { return self.PICModelArray1.count } if tableVIEw.isEqual(self.tableVIEw2) { return self.PICModelArray2.count } if tableVIEw.isEqual(self.tableVIEw3) { return self.PICModelArray3.count } return 10; } func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return 1; } // 设置cell的高度 func tableVIEw(tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGfloat { if tableVIEw.isEqual(self.tableVIEw1) { let model = self.PICModelArray1[indexPath.row] as PICModel let height = model.new_height as! CGfloat return height*0.5 } if tableVIEw.isEqual(self.tableVIEw2) { let model = self.PICModelArray2[indexPath.row] as PICModel let height = model.new_height as! CGfloat return height*0.5 } if tableVIEw.isEqual(self.tableVIEw3) { let model = self.PICModelArray3[indexPath.row] as PICModel let height = model.new_height as! CGfloat return height*0.5 } return 10; } func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { let IDentify:String = "PBtableVIEwCell" let cell:PBtableVIEwCell = tableVIEw.dequeueReusableCellWithIDentifIEr(IDentify) as! PBtableVIEwCell cell.selectionStyle = UItableVIEwCellSelectionStyle.None var model:PICModel! if tableVIEw.isEqual(self.tableVIEw1) { model = self.PICModelArray1[indexPath.row] as PICModel } if tableVIEw.isEqual(self.tableVIEw2) { model = self.PICModelArray2[indexPath.row] as PICModel } if tableVIEw.isEqual(self.tableVIEw3) { model = self.PICModelArray3[indexPath.row] as PICModel } cell.TitleLabel?.text = model.Title // 异步加载图片 dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0)) { let data = NSData.init(contentsOfURL: NSURL(string: model.pic_url_x!)!) dispatch_async(dispatch_get_main_queue(),{ cell.backImageVIEw?.image = UIImage.init(data: data!) }) } return cell } // 三个tableVIEw 关联滚动 func scrollVIEwDIDScroll(scrollVIEw: UIScrollVIEw) { if scrollVIEw.isEqual(self.tableVIEw1) { self.tableVIEw2?.setContentOffset((self.tableVIEw1?.contentOffset)!,animated: false) self.tableVIEw3?.setContentOffset((self.tableVIEw1?.contentOffset)!,animated: false) } if scrollVIEw.isEqual(self.tableVIEw2) { self.tableVIEw1?.setContentOffset((self.tableVIEw2?.contentOffset)!,animated: false) self.tableVIEw3?.setContentOffset((self.tableVIEw2?.contentOffset)!,animated: false) } if scrollVIEw.isEqual(self.tableVIEw3) { self.tableVIEw1?.setContentOffset((self.tableVIEw3?.contentOffset)!,animated: false) self.tableVIEw2?.setContentOffset((self.tableVIEw3?.contentOffset)!,animated: false) } } func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { print("row = \(indexPath.row)") } func tableVIEw(tableVIEw: UItableVIEw,TitleForDeleteConfirmationbuttonForRowAtIndexPath indexPath: NSIndexPath) -> String? { return "删除" } func tableVIEw(tableVIEw: UItableVIEw,commitEditingStyle editingStyle: UItableVIEwCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath){ self.indexPath = indexPath self.showAlVIEw(indexPath) } // UIAlertVIEw的使用 func showAlVIEw(indexPath :NSIndexPath){ let alert = UIAlertVIEw.init(Title: "警告",message: "确定删除第\(indexPath.row + 1)行",delegate: self,cancelbuttonTitle: "取消",otherbuttonTitles: "确定") alert.show() } func alertVIEw(alertVIEw: UIAlertVIEw,clickedbuttonAtIndex buttonIndex: Int) { if (buttonIndex == 0) { NSLog("取消"); } else{ NSLog("删除\(self.indexPath!.row)") } } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. }}
写在最后:@H_502_3@
可能是写的Swift的代码写少了,感觉写起来没有OC顺手,然后自己慢慢体会……O(∩_∩)O哈哈~@H_502_3@ 总结
以上是内存溢出为你收集整理的Swift UITableView瀑布流/NSURLConnection异步网络请求全部内容,希望文章能够帮你解决Swift UITableView瀑布流/NSURLConnection异步网络请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)