import UIKit class CountCollectionViewCell: UICollectionViewCell { @IBOutlet weak var label: UILabel! var mUtil = Util() override init(frame:CGRect) { super.init(frame: frame) } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } func setViewLabel(index:NSIndexPath){ self.label.text=mUtil.getCharacter(index) } }
import UIKit class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { @IBOutlet weak var textField: UITextField! @IBOutlet weak var mCollectionView: UICollectionView! var cell:CountCollectionViewCell? = nil var mUtil = Util() //当控制器的视图类加载完成时调用 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } //当系统触发内存警告时调用 override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //cell个数 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ return 15; } //相当于android中的getview func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as CountCollectionViewCell cell!.setViewLabel(indexPath) return cell! } //具体点击的哪个cell func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { textField.text = mUtil.getCharacter(indexPath) } }
IOS SWIFT---使用UICollectionView
原文地址:http://blog.csdn.net/u014163726/article/details/43762795