Swift自定义UITableView、UICollectionView
来源:互联网 发布:cad是什么软件 编辑:程序博客网 时间:2024/05/17 15:01
一、在工程里创建MyTableViewCell.swift文件
import UIKitclass MyTableViewCell: UITableViewCell { var model : Model? var backView : UIImageView? var titleLabel : UILabel? var timeLabel : UILabel? var likeLabel : UILabel? override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.backView = UIImageView(frame: CGRect(x: 10, y: 0, width: 355, height: 190)) self.backView?.backgroundColor = UIColor.yellowColor() self.backView?.layer.masksToBounds = true self.backView?.layer.cornerRadius = 10 self.addSubview(self.backView!) // 标题 self.titleLabel = UILabel(frame: CGRect(x: 10, y: 0, width: 355, height: 30)) self.titleLabel?.font = UIFont.systemFontOfSize(15) self.titleLabel?.backgroundColor = UIColor.cyanColor() self.addSubview(self.titleLabel!)// self.titleLabel?.text = self.model?.title // 喜欢 self.likeLabel = UILabel(frame: CGRect(x: 250, y: 160, width: 110, height: 30)) self.likeLabel?.backgroundColor = UIColor.blueColor() self.addSubview(self.likeLabel!) // 时间 self.timeLabel = UILabel(frame: CGRect(x: 10, y: 160, width: 100, height: 30)) self.timeLabel?.backgroundColor = UIColor.redColor() self.addSubview(self.timeLabel!) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state }}
二、在ViewController.swift
import UIKit// 网络请求import Alamofireclass ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // 定义属性 var myTableView : UITableView? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // 创建UITableView self.myTableView = UITableView(frame: UIScreen.mainScreen().bounds, style: .Plain) self.myTableView.delegate = self self.myTableView.dataSource = self self.view.addSubview(self.myTableView!) self.myTableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "reuse") // 网络请求 self.getAFNetworkingData() } func getAFNetworkingData() { Alamofire.request(.GET, "http://api.liwushuo.com/v2/channels/111/items?limit=20&offset=0&gender=1") .responseJSON {response in debugPrint(response) } } //MARK: UITableViewDataSource func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("reuse", forIndexPath: indexPath) as! MyTableViewCell configureCell(cell, forRowAtIndexPath: indexPath) return cell } func configureCell(cell: MyTableViewCell, forRowAtIndexPath: NSIndexPath) {// cell.textLabel?.text = MyTableViewCell().model?.title// cell.imageView?.image = UIImage(named: "2.jpg") cell.model = MyTableViewCell().model } // 高度 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 200 } // 点击进入下一界面 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.presentViewController(FirstViewController(), animated: true) { () -> Void in } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
三、下一界面FirstViewController.swift
import UIKitclass FirstViewController: UIViewController { var myButton : UIButton? // 自定义View var myView : BackGroundView? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.view.backgroundColor = UIColor.whiteColor() self.myButton = UIButton(type: .Custom) self.myButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100) self.myButton.setTitle("返回", forState: .Normal) self.myButton.backgroundColor = UIColor.grayColor() self.view.addSubview(self.myButton!) self.myButton.addTarget(self, action: "tap:", forControlEvents: .TouchUpInside) self.myView = BackGroundView(frame: CGRect(x: 0, y: 110, width: self.view.frame.size.height, height: 400)) self.view.addSubview(self.myView!) } func tap(sender : AnyObject) { self.dismissViewControllerAnimated(true) { () -> Void in } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */}
四、自定义BackGroundView.swift里嵌套UICollectionView
import UIKitclass BackGroundView: UIView, UICollectionViewDelegate, UICollectionViewDataSource { var flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout() var collectionView : UICollectionView? override init(frame: CGRect) { super.init(frame: frame) flowLayout.itemSize = CGSize(width: frame.size.width, height: frame.size.height) flowLayout.scrollDirection = .Horizontal flowLayout.minimumInteritemSpacing = 0 flowLayout.minimumLineSpacing = 0 self.collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: self.flowLayout) self.collectionView?.delegate = self self.collectionView?.dataSource = self self.addSubview(self.collectionView!) self.collectionView?.registerClass(BackCollectionViewCell.self, forCellWithReuseIdentifier: "reuse") } // 类中存在异于父类的初始化方法必须要写 // required required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } // MARK: UICollectionViewDataSource func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 100 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("reuse", forIndexPath: indexPath) as! BackCollectionViewCell configureCell(cell, forItemAtIndexPath: indexPath) return cell } func configureCell(cell: BackCollectionViewCell, forItemAtIndexPath: NSIndexPath) { cell.backgroundColor = UIColor.redColor() } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */}
五、自定义BackCollectionViewCell.swift
import UIKitclass MyModel : NSObject { var imageUrl : String? init(imageUrl : String) { self.imageUrl = imageUrl }}class BackCollectionViewCell: UICollectionViewCell { var myView : UIImageView! var model : MyModel? { willSet (newValue) { print(newValue) print(model) } didSet { print(model?.imageUrl) self.myView?.image = UIImage(named: (model!.imageUrl)!) } } override init(frame: CGRect) { super.init(frame: frame) self.myView = UIImageView(frame: self.bounds) self.myView.image = UIImage(named: "1.jpg") self.addSubview(self.myView) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }}
0 0
- Swift自定义UITableView、UICollectionView
- IOS控件系列--对象池模式的应用--使用UIScrollView自定义设计滚动列表(UITableView、UICollectionView)(Swift版)
- Swift UITableView使用自定义cell
- swift UItableView和自定义uitableViewCell
- swift UICollectionView 代码创建 和自定义Cell
- iOS开发Swift UITableView和UICollectionView的使用
- swift之自定义表格控件(UITableView)
- Swift中UITableView的用法(自定义UITableView和UITableViewCell)
- iOS UICollectionView与UITableView
- UITableView UICollectionView经验积累
- iOS UITableView、UICollectionView、UIWebView
- UITableView 和 UICollectionView
- ios8 Swift UICollectionView Header 常驻(如UITableView 的Header效果)实现
- iOS swift-UIScrollview,UITableview,UICollectionView单独禁止下拉(上拉)
- Swift UICollectionView、UITableView列点击事件与 UITapGestureRecognizer 点击事件冲突处理
- swift uitableview
- swift UItableView
- UITableView --Swift
- mysql安装配置问题相关
- Tomcat中更改网站根目录和默认页的配置方法
- ubuntu14.04解决记事本乱码问题
- H264 NAL头解析
- 让git忽略掉文件权限检查
- Swift自定义UITableView、UICollectionView
- 【C#】利用C#窗体与SQL Server的连接、Treeview制作SQL Server数据库查看器
- 判断字符串是否为null,使用 == 号
- iOS使用命令行进行企业版打包发布
- iOS关键字strong/weak/assign/retain/nonatomic/atomic
- [SDCC讲师专访]阿里王晶昱:云时代的分布式数据库DRDS
- jabref【参考文献生成,抓去,推送,管理工具】简介
- android 之 Camera
- AS通过git clone 项目代码