John John - 1 month ago 11
Swift Question

Get Indexpath Value in UiCollectionViewCell Class

I have a UiCollectionViewCell Class as follow:

class DateCollectionViewCell: UICollectionViewCell {

let dateLabel = UILabel()
override func awakeFromNib() {
super.awakeFromNib()

dateLabel.frame = CGRectMake(0, 5, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))
dateLabel.textAlignment = .Left
dateLabel.font = UIFont(name: "HelveticaNeue", size: 13)

dateLabel.adjustsFontSizeToFitWidth = false
self.contentView.addSubview(dateLabel)

}
}


I want to change the Y-positon of my first row first column cell label only, rest of the cells label remain unchanged.So for this I need IndexPath value so I can check for section and row of current cell.

How can I get this ??
Is there any other way to do what I want to get ???

SMi SMi
Answer
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

   let cell : DateCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(CellIdentifier, forIndexPath: indexPath) as! DateCollectionViewCell 

   if indexPath.section == 0 && indexPath.row == 0  {
        // do your custom changes for the cell
    }

   return cell // your default cell
}
Comments