技术文章分类(180)

技术随笔(11)

swift UICollectionViewController操作

1,这是UICollectionView单独做Controller时的用法

跟UItableview不同的地方,collectionview需要设置FlowLayout

//
//  WeatherCollectionViewController.swift
//  SwiftStudy3
//
//  Created by dongway on 14-7-11.
//  Copyright (c) 2014年 dongway. All rights reserved.
//

import UIKit

let reuseIdentifier = "cell"

class WeatherCollectionViewController: UICollectionViewController {
    
    init(){
        var layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(30,30)
        var paddingY:CGFloat = 10
        var paddingX:CGFloat = 10
        layout.sectionInset = UIEdgeInsetsMake(5,5,5,5)
        layout.minimumLineSpacing = paddingY
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView.backgroundColor = UIColor.greenColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // #pragma mark UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return 50
    }

    override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell? {
        let cell = collectionView!.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
        cell.backgroundView = UIImageView(image: UIImage(named: "weather.png"))
        // Configure the cell
        return cell
    }
    
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(collectionView: UICollectionView!, shouldSelectItemAtIndexPath indexPath: NSIndexPath!) -> Bool {
        println("selected")
        return true
    }
}

如果我要在其他页面调用这个collectionview:

     var weatherCollectionViewController:WeatherCollectionViewController = WeatherCollectionViewController()
        weatherCollectionViewController.view.frame = CGRectMake(0,0,DeviceFrame.size.width,200)
        self.addChildViewController(weatherCollectionViewController)
        self.addSubview(weatherCollectionViewController.view)

 

posted @ 2014-07-11 14:18  坤哥MartinLi  阅读(2057)  评论(0编辑  收藏  举报