具有多个部分和自定义部分 header 的 UITableView,具有单个单元格选择和多个单元格选择

UITableView with multiple sections and custom section header with single cell selection and multiple cell selection

我是设置UITableView,其中有多个部分,一节有一个选择,第二部分选择了UberEats在add-ons和类型的类型时,需要完全选择UberEats食物

我已经展开和关闭单元格行,我想更新自定义标签 header

var selectedIndexPath: IndexPath?

cellForRowAt

if let inxPath = selectedIndexPath{
            if inxPath.section == 0{
                if inxPath == indexPath{
                    if inxPath.row == indexPath.row && inxPath.section == indexPath.section{
                        cell.radioButtonImageView.image = #imageLiteral(resourceName: "radioCheck")
                    }
                }
            }
            if inxPath.section == 1{
                if inxPath.row == indexPath.row && inxPath.section == indexPath.section{
                    if cell.radioButtonImageView.image == #imageLiteral(resourceName: "IconUnmarked"){
                        cell.radioButtonImageView.image = #imageLiteral(resourceName: "IconMarked")
                    }else if cell.radioButtonImageView.image == #imageLiteral(resourceName: "IconMarked"){
                        cell.radioButtonImageView.image = #imageLiteral(resourceName: "IconUnmarked")
                    }
                }

            }

        }

didSelectRowAt

selectedIndexPath = indexPath
tableView.reloadData()

创建一个 int 和 int 实例属性数组来存储所选行的详细信息。

var selectedOfferIndex: Int? // section 0
var selectedItemIndices: [Int] = []//section 1

在 cellForRowAt 中比较所选值并更改 UI

if section == 0 {
    if indexPath.row == selectedOfferIndex {
        //...
    } else {
        //...
    }
} else {//section 1
    if selectedItemIndices.contains(indexPath.item) {
        //...
    } else {
        //...
    }
}

在 didSelectRowAt 方法中更新选择的值

if section == 0 {
    selectedOfferIndex = indexPath.row
} else {//section 1
    if index = selectedItemIndices.firstIndex(of: indexPath.row) {
        selectedItemIndices.remove(at: index)
    } else {
        selectedItemIndices.append(indexPath.row)
    }
}
tableView.reloadData()