【问题标题】:How to present UIAlertController when finished multiple selection in UICollectionView在 UICollectionView 中完成多项选择后如何呈现 UIAlertController
【发布时间】:2022-01-11 12:09:13
【问题描述】:
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()
    }
}

我的目标是在集合视图中完成多项选择时显示警报 提前谢谢你:)

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    您的描述有点单薄,所以我猜测/假设您想要的是那种;用户在每个部分中选择一个项目后,应显示警报视图。

    要实现这一点,您可以为每个可能的选择设置一个可为空的属性,然后检查它们是否都已设置。例如想象有

    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()
    

    我希望这能让你走上正轨。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多