Skip to content

Instantly share code, notes, and snippets.

@tomekc
Created August 3, 2014 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomekc/becfdf6601ba64d5fd5d to your computer and use it in GitHub Desktop.
Save tomekc/becfdf6601ba64d5fd5d to your computer and use it in GitHub Desktop.
Trouble with UICollectionViewCell reuse
//
// ViewController.swift
// NewHiringSwift
//
// Created by Tomek on 01.08.2014.
// Copyright (c) 2014 Tomek Cejner. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.collectionView.backgroundColor = UIColor(red: 1.0, green: 0.5, blue: 0.0, alpha: 1.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 10
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.greenColor()
if let labelka = cell.viewWithTag(100) as? UILabel {
labelka.text = String(format: "Row %d", indexPath.row)
}
// list subviews
NSLog("--- ROW %d ---", indexPath.row)
printSubviews(cell)
return cell
}
func printSubviews(view:UIView) {
if let list = view.subviews as? [UIView] {
for uiv in list {
NSLog("%@ tag:%d", uiv, uiv.tag)
printSubviews(uiv)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment