Im trying to preselect the first object/UICollectionViewCell in the UICollectionView? I have tried:
self.dateCollectionView.allowsMultipleSelection=NO;
[self.da
For Swift 3.0.1 You can try this:
self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
or
self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition(rawValue: 0))
For Objective-C You can try this:
self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
Note: You should use it in viewDidAppear
I solved this by subclassing UICollectionView
and selecting needed item in layoutSubviews
:
class InitialSelectionCollectionView: UICollectionView {
var initialSetupPerformed: Bool = false
var initialSelectedIndexPath: IndexPath!
override func layoutSubviews() {
super.layoutSubviews()
if !initialSetupPerformed && initialSelectedIndex != nil{
selectItem(at: initialSelectedIndexPath, animated: false, scrollPosition: .centeredHorizontally)
initialSetupPerformed = true
}
}
}
Then, when you init your custom collection view, just set needed IndexPath
to initialSelectedIndexPath
For swift 3
Use collectionView.selectItem
with-in this overload collectionView(UICollectionView, IndexPath)
This is my code, in this code I pre selected row with indexPath.row = 0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ScenarioCollectionView.dequeueReusableCell(withReuseIdentifier: "ReuseScenarioCollectionViewCell", for: indexPath as IndexPath) as! ScenarioCollectionViewCell
if (indexPath.row == 0){
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
cell.layer.borderColor=UIColor.gray.cgColor
}else{
cell.layer.borderColor=UIColor.white.cgColor
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderColor = UIColor.gray.cgColor
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath as IndexPath)
cell?.layer.borderColor = UIColor.white.cgColor
collectionView.deselectItem(at: indexPath, animated: true)
}
For Swift 3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//auto selected 1st item
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
self.collectionView?.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: .top)
}
swift code for selecting a cell initially
func selectinitialCell() {
let cell = venueTypeCollectionView.cellForItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! SearchTypeCollectionViewCell
self.indexPathOfSelectedItem = NSIndexPath(forItem: 0, inSection: 0)
cell.venueType.textColor = UIColor.whiteColor()
cell.selected = true
}
but this does not invoke the delegate function such as didSelectItemAtIndexPath, didDeselectItemAtIndexPath.
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded]; //important
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];