pre select/highlight UICollectionViewCell on first load of view
Solution 1
In viewDidAppear
:
NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection:0];
[self.dateCollectionView selectItemAtIndexPath:indexPathForFirstRow animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.dateCollectionView didSelectItemAtIndexPath:indexPathForFirstRow];
Solution 2
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)
}
Solution 3
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
Solution 4
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
Solution 5
For me, putting it in viewDidAppear:
cause a second to select, so the user will see both states (i.e. not selected, and selected).
To avoid this, I put it in viewWillAppear:
instead and worked like a charm
override func viewWillAppear(_ animated: Bool) {
let selectedIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left)
}
Comments
-
DevC over 3 years
Im trying to preselect the first object/UICollectionViewCell in the UICollectionView? I have tried:
self.dateCollectionView.allowsMultipleSelection=NO; [self.dateCollectionView selectItemAtIndexPath:0 animated:YES scrollPosition:UICollectionViewScrollPositionLeft]; [self collectionView:self.dateCollectionView didSelectItemAtIndexPath:0]; [self.dateCollectionView reloadData];
in
viewDidLoad
.Here are my
UICollectionView
methods;-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.titles.count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.backgroundColor= [UIColor clearColor]; UILabel * dateLabel = (UILabel *)[cell viewWithTag:1]; UILabel * subText = (UILabel *)[cell viewWithTag:2]; subText.text=self.titles[indexPath.row]; subText.adjustsFontSizeToFitWidth=YES; if (cell.selected) { cell.backgroundColor = [UIColor blueColor]; // highlight selection } else { cell.backgroundColor = [UIColor redColor]; // Default color } return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath]; datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection } -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath]; datasetCell.backgroundColor = [UIColor redColor]; // Default color } - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath; { return YES; }