我有一个UICollectionView
。 UICollectionView
的datasource
是学生的NSArray
(来自CoreData
)。我希望当前的学生项目为selected
/ highlighted
。
我该怎么做?我知道有一种方法:
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
[NSIndexPath
和UICollectionViewScrollPosition
作为参数。
我有数据源(从NSArray
填充学生的CoreData
,而学生对象是selected
。
所以,如何获得NSIndexPath
和UICollectionViewScrollPosition
?还是有其他方法可以突出显示项目?
您可以在[self.collectionView reloadData]
之后简单地使用它
[self.collectionView
selectItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]
animated:YES
scrollPosition:UICollectionViewScrollPositionCenteredVertically];
其中index
是所选学生的索引号。
Swift
let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)
[根据您的问题,我假设您只有一个选定的学生,我做了类似的事情,用户可以选择一系列图标。首先考虑到负载,我做了:
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)
}
这里我默认选择第一个单元格,您将使用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
}
首先显示集合,然后对选择的更改做出反应时会调用此方法:
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()
}
显然,有很多显示单元格选择的方法,我的方法很简单,但这就是我要做的全部。
编辑:自发布以上内容以来,我已经认识到,只需将观察者添加到单元格类中的selected
会更简单:
class IconCollectionViewCell: UICollectionViewCell {
...
override var selected: Bool {
didSet {
backgroundColor = selected ? UIColor.grayColor() : UIColor.clearColor()
}
}
}
将其放置在适当的位置,无需处理didSelect
或didDeselect
或检查在cellForItemAtIndexPath
中选择的内容,单元格会自动执行。
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
paymentHeaderCollectionView.selectItem(at: indexPathForFirstRow, animated: false, scrollPosition: UICollectionViewScrollPosition.left)
self.collectionView(paymentHeaderCollectionView, didSelectItemAt: indexPathForFirstRow)
签名已更改。
didSelectItemAtIndexPath to didSelectItemAt
您请检查此内容:
_ collectionView
请尝试:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
print(" selected",)
}