func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ShiftCollectionViewCell.identifier, for: indexPath) as? ShiftCollectionViewCell else {
return UICollectionViewCell()
}
let model = shiftSection[indexPath.section].options[indexPath.row]
cell.configure(withModel: OptionsCollectionViewCellViewModel(id: 0, data: model.title))
return cell
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
collectionView.indexPathsForSelectedItems?.filter({ $0.section == indexPath.section }).forEach({ collectionView.deselectItem(at: $0, animated: false) })
return true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let model = shiftSection[indexPath.section].options[indexPath.row]
print(model.title)
if indexPath.section == 2 {
showAlert()
}
}

我的目標是在 collectionview 中完成多項選擇時顯示警報 提前謝謝:)
uj5u.com熱心網友回復:
你的描述有點單薄,所以我猜測/假設你想要的是那種;用戶在每個部分中選擇一個專案后,應顯示警報視圖。
為了實作這一點,您可以為每個可能的選擇設定一個可為空的屬性,然后檢查是否設定了所有選項。例如想象有
private var timeMode: TimeMode?
private var shift: Shift?
private var startTime: StartTime?
現在,didSelectItemAt您將嘗試填充這些屬性,例如:
if indexPath.section == 0 { // TODO: rather use switch statement
timeMode = allTimeModes[indexPath.row]
} else if indexPath.section == 1 {
shift = allShifts[indexPath.row]
} ...
然后在這個方法的末尾(最好呼叫一個新方法)執行一個檢查
guard let timeMode = self.timeMode else { return }
guard let shift = self.shift else { return }
guard let startTime = self.startTime else { return }
showAlert()
或者
您可以使用集合視圖屬性indexPathsForSelectedItems來確定每次用戶選擇某些內容時都以類似方式選擇的內容:
guard let timeModeIndex = collectionView.indexPathsForSelectedItems?.first(where: { $0.section == 0 })?.row else { return }
guard let shiftIndex = collectionView.indexPathsForSelectedItems?.first(where: { $0.section == 1 })?.row else { return }
guard let startTimeIndex = collectionView.indexPathsForSelectedItems?.first(where: { $0.section == 2 })?.row else { return }
showAlert()
我希望這能讓你走上正軌。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/375494.html
上一篇:iOS13UIToolBar樣式
