【问题标题】:Single and MultiSelection cells in same tableView | Swift同一个 tableView 中的 Single 和 MultiSelection 单元格 |迅速
【发布时间】:2021-09-22 13:21:51
【问题描述】:

在复制这个问题之前,请注意我已经在这个问题上花费了几天时间,工作时间,并在 SO 上寻找所有相同类型的问题,但是我遗漏了一些东西或做错了。

我有一个通过 API 响应填充数据的 tableView。下面是我的模型。

struct Model : Codable {
   let bugClassification : [Bug]?
}

struct Bug : Codable {
  let selectable : String?  //Telling wether cell is single/Multi selected
  var options : [Options]?
}

struct Options : Codable, Equatable {
  let title : String?
  let id: Int
  var isCellSelected: Bool = false
}

场景 我想创建多个部分,每个部分都有不同的单元格,具体取决于可选择的类型,单个或多个。我已经做到了,但我遇到的问题是,每当我滚动时,也会选择随机单元格。现在,我知道这种行为是因为 tableView 重用了单元格。但我对如何处理这一切感到困惑。另外,我想对部分进行验证,也就是说,每个部分都应该至少选择一个单元格。请指导我正确的方向,任何小的帮助将不胜感激。下面是我的代码。

CellForRowAt

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if bugClassification[indexPath.section].selectable?.lowercased() == "multi-select" {
        //Multi-Selection
        let cell = tableView.dequeueReusableCell(withIdentifier: multiSelectionCellID) as! MultiSelectionCell
        let item = bugClassification[indexPath.section].options![indexPath.row]
        cell.label.text = item.title
        if item.isCellSelected {
            cell.checkMarkImageView.alpha = 1
            cell.checkMarkView.layer.borderColor = UIColor.white.cgColor
            cell.checkMarkView.backgroundColor = .emerald
        } else if item.isCellSelected {
            cell.checkMarkImageView.alpha = 0
            cell.checkMarkView.layer.borderColor = UIColor.veryLightBlue.cgColor
            cell.checkMarkView.backgroundColor = .white
        }
        return cell
    } else {
        //Single-Selection
        let cell = tableView.dequeueReusableCell(withIdentifier: singleSelectionCellID) as! SingleSelectionCell
        let item = bugClassification[indexPath.section].options![indexPath.row]
        cell.label.text = item.title
        if item.isCellSelected {
            cell.checkMarkImageView.alpha = 1
            cell.checkMarkView.layer.borderColor = UIColor.emerald.cgColor
        } else {
            cell.checkMarkImageView.alpha = 0
            cell.checkMarkView.layer.borderColor = UIColor.veryLightBlue.cgColor
        }
        return cell
    }
}

DidSelectRow 方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if bugClassification[indexPath.section].selectable?.lowercased() == "multi-select" {
        var item = bugClassification[indexPath.section].options![indexPath.row]
        item.isCellSelected = !item.isCellSelected
        bugClassification[indexPath.section].options![indexPath.row] = item
        self.tableView.reloadRows(at: [indexPath], with: .automatic)
       
    } else {
        let items = bugClassification[indexPath.section].options
        if let selectedItemIndex = items!.indices.first(where: { items![$0].isCellSelected }) {
            bugClassification[indexPath.section].options![selectedItemIndex].isCellSelected = false
            if selectedItemIndex != indexPath.row {
                bugClassification[indexPath.section].options![indexPath.row].isCellSelected = true
            }
        } else {
            bugClassification[indexPath.section].options![indexPath.row].isCellSelected = true
        }
        self.tableView.reloadSections([indexPath.section], with: .automatic)
    }
}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    在 cellForRowAt 中

    if item.isCellSelected == true{
                    cell.accessoryType = .checkmark
                } else {
                    cell.accessoryType = .none
                }
    

    并通过每次选择更新模型

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let item = bugClassification[indexPath.section].options![indexPath.row]
                        if let cell = tableView.cellForRow(at: indexPath) {
                            cell.accessoryType = .none
                            if indexPath.section == 0{
                                item.isCellSelected.isSelected = false
                            }else{
                                item.isCellSelected.isSelected = false
                            }
                        }
                    }
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = bugClassification[indexPath.section].options![indexPath.row]
                        if let cell = tableView.cellForRow(at: indexPath) {
                            cell.accessoryType = .checkmark
                            if indexPath.section == 0{
                                item.isCellSelected.isSelected = true
                            }else{
                                item.isCellSelected.isSelected = true
                            }
                        }
                    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2018-05-18
      • 2021-05-01
      • 1970-01-01
      相关资源
      最近更新 更多