如何以编程方式选择collectionview中的项目?

mrd*_*mrd 21 iphone ios uicollectionview uicollectionviewcell

我有一个UICollectionView.该UICollectionViewdatasourceNSArray学生(从CoreData).我希望当前的学生项目是selected/ highlighted.

我怎样才能做到这一点?我知道有一种方法:

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                     animated:(BOOL)animated 
               scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
Run Code Online (Sandbox Code Playgroud)

作为参数a NSIndexPathUICollectionViewScrollPosition.

我有数据源(NSArray由学生填充CoreData)和学生对象selected.

那么,我怎么得到NSIndexPathUICollectionViewScrollPosition?或者有没有其他方法来突出显示项目?

Sha*_*hin 31

你可以在之后使用它 [self.collectionView reloadData]

[self.collectionView 
   selectItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] 
                animated:YES
          scrollPosition:UICollectionViewScrollPositionCenteredVertically];
Run Code Online (Sandbox Code Playgroud)

index所选学生的索引号在哪里.

  • 晚到晚会,但它可以帮助人们.从Apple的`optional func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath)`的文档中,当集合用户在集合视图中成功选择一个项目时,集合视图会调用此方法.以编程方式设置选择时,它不会调用此方法._ (7认同)

Kru*_*nal 13

迅速

let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)
Run Code Online (Sandbox Code Playgroud)


sgi*_*gib 7

从你的问题我假设你只有一个选定的学生,我用一组用户可以选择的图标做了类似的事情.首先在视图中我做了加载:

override func viewDidLoad() {
    super.viewDidLoad()

    iconCollectionView.delegate = self
    iconCollectionView.dataSource = self
    iconCollectionView.allowsMultipleSelection = false
    iconCollectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: false, scrollPosition: .None)
}
Run Code Online (Sandbox Code Playgroud)

在这里,我默认选择第一个单元格,您将用于StudentArray.indexOf获取您选择的学生索引.然后,为了显示我选择了哪个项目:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = iconCollectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! IconCollectionViewCell
    cell.imageView.image = UIImage(named: imageResourceNames.pngImageNames[indexPath.row])
    if cell.selected {
        cell.backgroundColor = UIColor.grayColor()
    }
    return cell
}
Run Code Online (Sandbox Code Playgroud)

首次显示集合时调用此方法,然后对选择中的更改做出反应:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.clearColor()
}
Run Code Online (Sandbox Code Playgroud)

显然有很多方法可以显示细胞选择,我的方式很简单,但这就是我需要做的.

编辑:自从发布以上内容后,我意识到selected在单元类中添加一个观察者更简单:

class IconCollectionViewCell: UICollectionViewCell {

...

    override var selected: Bool {
        didSet {
            backgroundColor = selected ? UIColor.grayColor() : UIColor.clearColor()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个地方有没有需要处理didSelectdidDeselect或为您在选择cellForItemAtIndexPath,细胞自动执行它.