I am loading a collection view where upon cell selection the cell draws a circle (UIView + CGRect) on it's view to indicate the selection. This collection view represents a calendar and I want the first date to be selected when initially loaded. For the life of me cannot get this is load properly, either the code for the selection does not fire, or the circle get's drawn incorrectly because the outlet's have not been initialized on the cell (the circle's position is reliant on the position of a label).
This is my select method on the cell, that is called on cell selection from the collection view data source:
func select() {
circleView = UIView(frame: CGRectMake(dateLabel.center.x - (dateLabel.frame.size.width / 2), dateLabel.center.y - (dateLabel.frame.size.height / 2), 40, 40))
circleView!.layer.cornerRadius = 20
circleView!.backgroundColor = UIColor.blueColor()
insertSubview(circleView!, atIndex: 0)
isCellSelected = true
}
Here is my code for didSelectItemAtIndexPath:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE MMMM dd, yyyy"
var dateString = ""
if let newDate = NSCalendar.currentCalendar().dateFromComponents(monthComponentsByDay[indexPath.row]) {
dateString = dateFormatter.stringFromDate(newDate)
delegate?.dateSelected(newDate)
}
if currentCell != nil {
currentCell!.deselect()
currentCell = nil
}
dateLabel.text = dateString
currentCell = collectionView.cellForItemAtIndexPath(indexPath) as? CalendarCollectionViewCell
currentCellIndex = indexPath.row
currentCell!.select()
}
I have tried collectionView.selectItemAtIndexPath(indexPath:animated:scrollPosition:) then manually calling the code to select, but the cell has not completely loaded at that time. I tried calling this in cellForItemAtIndexPath and viewDidLoad of the view controller with no luck.
Any suggestions on how to select an cell on load would be great! Thanks.
0 comments:
Post a Comment