一、在工程里创建MyTableViewCell.swift文件
import UIKit
class 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 Alamofire
class 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 UIKit
class 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 UIKit
class 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 UIKit
class 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")
}
}