2015-05-22 16 views
12

Koleksiyon görünümünü programlı olarak yapıyorum. İşte viewDidLoad fonkUICollectionView'da atIndex hücresi nasıl hızlı bir şekilde alınır?

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) 
    layout.itemSize = CGSize(width: 90, height: 120) 
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    collectionView!.dataSource = self 
    collectionView!.delegate = self 
    collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell") 
    collectionView!.layer.cornerRadius = 2 
    collectionView!.backgroundColor = UIColor.redColor() 
    self.containerView.addSubview(collectionView!) 

Ve bunlar benim toplama görünümü fonksiyonları

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 8 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell 
    cell.backgroundColor = UIColor.blackColor() 
    cell.buttonView.addTarget(self, action: Selector("Action\(indexPath.row):"), forControlEvents: UIControlEvents.TouchUpInside) 
    return cell 
} 

Fikir olduğu kod olduğunu ben hücrede bir düğmesi vardır ve bunu bastığımda hücre rengini değiştirmek gerekir, burada eylem işlevi

func Action0(sender: UIButton!) { 
    var indexPath = NSIndexPath(forRow: 0, inSection: 0) 
    let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell 
    cell.backgroundColor = UIColor.blueColor() 

ve bu, collectionViewCell sınıftır

class CollectionViewCell: UICollectionViewCell { 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

var buttonView: UIButton! 
override init(frame: CGRect) { 
    super.init(frame: frame) 

    buttonView = UIButton.buttonWithType(UIButtonType.System) as! UIButton 
    buttonView.frame = CGRect(x: 0, y: 16, width: frame.size.width, height: frame.size.height*2/3) 

    contentView.addSubview(buttonView) 
} 

}

düğmesi ve eylem işleri ancak hücre rengini değiştirmez, ben sorunun bu satırda olduğunu tahmin

let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell 

cevap

36

Bu hücreyi almak için bir yoldur verilen indexPath:

let cell = collectionView!.cellForItemAtIndexPath(indexPath) 

Ancak, aynı zamanda denemek isteyebilirsiniz:

cell.contentView.backgroundColor = UIColor.blueColor() 

NOT:

olsa yukarıda aynı işlevselliği elde etmek için farklı bir uygulama denemek için teşvik etmek istiyorum çalışmış olabilir. Uygulamanız, her bir CollectionViewCell için ayrı bir Action # fonksiyonuna sahip olmanızı ve indeksPath'in bu yöntemlerin her birinde manuel olarak oluşturmanızı gerektirecektir.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell 
    cell.backgroundColor = UIColor.blackColor() 
    cell.buttonView.addTarget(self, action: Selector("action"), forControlEvents: UIControlEvents.TouchUpInside) 
    return cell 
} 

ve bu bir yöntemi için fonksiyon olacaktır gibi bir şey:

func action(sender: UIButton!) { 
    var point : CGPoint = sender.convertPoint(CGPointZero, toView:collectionView) 
    var indexPath = collectionView!.indexPathForItemAtPoint(point) 
    let cell = collectionView!.cellForItemAtIndexPath(indexPath) 
    cell.backgroundColor = UIColor.blueColor() 
} 

Sadece bir öneri! Mutlu kodlar! :)

+0

Mükemmel! Teşekkür ederim! – Jeff

0

Birlikte satır alabilirsiniz cell.selected denemek ve daha sonra Swift 2 renk

0

değiştirin:

let point : CGPoint = sender.convertPoint(CGPointZero, toView:myTableView) 
let indexPath = myTableView.indexPathForRowAtPoint(point) 
let cell = myTableView.cellForRowAtIndexPath(indexPath!) 
+0

Bu kod UITableView içindir, UICollectionView, asker istediği gibi değildir. – benhameen

İlgili konular