[英]Set default cell in a UICollectionView (SWIFT)?
I'm a Swift beginner trying to implement a way to set a default cell in a CollectionView when the app first launches.我是一名 Swift 初学者,试图在应用首次启动时实现一种在 CollectionView 中设置默认单元格的方法。 I have overriden my 'isSelected' method to graphically show on the screen the selected cell eg;我已经覆盖了我的“isSelected”方法,以图形方式在屏幕上显示选定的单元格,例如; borderColor, etc...边框颜色等...
My approach (rightly or not) so far is to call the 'didSelectItemAt' method inside my 'viewDidAppear' method.到目前为止,我的方法(正确与否)是在我的“viewDidAppear”方法中调用“didSelectItemAt”方法。
I understand that the 'didSelectItemAt' method expects a CollectionView as its first argument, but not matter what I put as the first argument when I call the method inside my 'viewDidAppear' nothing seems to work.我知道'didSelectItemAt'方法需要一个CollectionView作为它的第一个参数,但是当我在我的'viewDidAppear'中调用该方法时,不管我把什么作为第一个参数,似乎没有任何效果。
any ideas?有任何想法吗?
extension DrinkMenuViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkTypeImageCollectionViewCell", for: indexPath) as! DrinkTypeImageCollectionViewCell
cell.imgDrinkType.image = imageArray[indexPath.row]
cell.lbDrinkType.text = drinkTypeArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkTypeImageCollectionViewCell", for: indexPath) as! DrinkTypeImageCollectionViewCell
cell.isSelected = true
print("selected cell: ", indexPath)
}
override func viewDidAppear(_ animated: Bool) {
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
collectionView(<#T##collectionView: UICollectionView##UICollectionView#>, didSelectItemAt: indexPathForFirstRow)
print (indexPathForFirstRow)
}
}
You are not the one to explicitly call the delegate methods of any API. 您不是显式调用任何API的委托方法的人。
func collectionView(_:didSelectItemAt:)
is the delegate method of UICollectionView
. func collectionView(_:didSelectItemAt:)
是UICollectionView
的委托方法。 The api itself handles when to call this method. api本身会处理何时调用此方法。 So you must not call this method from anywhere in your code. 因此,您不得在代码中的任何位置调用此方法。
Again your implementation of func collectionView(_:didSelectItemAt:)
doesn't make any sense. 同样,您对func collectionView(_:didSelectItemAt:)
实现没有任何意义。 This method will automatically be called when you tap on any of your cell. 当您点击任意一个单元格时,将自动调用此方法。 You don't have to deque
your cell again here. 您不必到deque
再次在这里你的。
You are probably asking for: pre select/highlight UICollectionViewCell on first load of view您可能要求: 在第一次加载视图时预选择/突出显示 UICollectionViewCell
Try this:试试这个:
override func viewWillAppear(_ animated: Bool) {
let selectedIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.