首页 > 解决方案 > 具有多个部分的 UITableView 和具有单个单元格选择和多个单元格选择的自定义部分标题

问题描述

我正在设置UITableView多个部分,第一个部分有一个选择,第二个部分有多个选择它想要在UberEats选择附加组件和食物类型时 发生什么

我已经扩展和关闭单元格行,我想更新自定义标题中的标签

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

标签: iosuitableviewswift4

解决方案


创建一个 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()

推荐阅读