在第一次加载时预先select/高亮显示UICollectionViewCell

我试图预先selectUICollectionView中的第一个对象/ UICollectionViewCell? 我努力了:

self.dateCollectionView.allowsMultipleSelection=NO; [self.dateCollectionView selectItemAtIndexPath:0 animated:YES scrollPosition:UICollectionViewScrollPositionLeft]; [self collectionView:self.dateCollectionView didSelectItemAtIndexPath:0]; [self.dateCollectionView reloadData]; 

viewDidLoad

这里是我的UICollectionView方法;

  -(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; } 

viewDidAppear

 NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection:0]; [self.dateCollectionView selectItemAtIndexPath:indexPathForFirstRow animated:NO scrollPosition:UICollectionViewScrollPositionNone]; [self collectionView:self.dateCollectionView didSelectItemAtIndexPath:indexPathForFirstRow]; 

快3

在这个重载collectionView(UICollectionView, IndexPath)使用collectionView.selectItem

这是我的代码,在这个代码中,我预选了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) } 

对于Swift 3.0.1你可以试试这个:

 self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: []) 

要么

 self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition(rawValue: 0)) 

对于Objective-C,你可以试试这个:

 self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone]; 

注意:你应该在viewDidAppear中使用它

对于Swift 3:

 override func viewDidAppear(_ animated: Bool) { //auto selected 1st item let indexPathForFirstRow = IndexPath(row: 0, section: 0) self.collectionView?.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: .top) } 

用于初始select单元的swift代码

 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 } 

但是这不会调用委托函数,如didSelectItemAtIndexPath,didDeselectItemAtIndexPath。

我通过UICollectionView并在layoutSubviewsselect需要的项目来解决这个问题:

 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 } } } 

然后,当你初始化自定义集合视图时,只需将IndexPath设置为initialSelectedIndexPath

对我来说,把它放在viewDidAppear()导致第二个select,所以用户将看到两个状态(即没有select,并select)。 为了避免这一点,我把它放在viewWillAppear()中,而是像魅力一样工作

 override func viewWillAppear(_ animated: Bool) { let selectedIndexPath = IndexPath(item: 0, section: 0) collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left) }