swift - iOS 13 UITableView 选择/取消选择问题

标签 swift uitableview select ios13 uiswitch

我的项目 UITableView 在 ios 12 及更低版本上运行良好,但随着 ios 13 的新更新,它开始出现多个问题。

选择的工作方式是我得到最后一个索引路径,但我在代码中对于所选行我更改了背景,现在背景为我选择的每一行而不仅仅是最后一行保持选中状态,并且我确实设置了我可以仅选择一行。

如果我滚动索引路径 3 处的选定行,例如具有不同的背景,并且当我滚动时,因为表有 90 行,所以每 10 或 15 行也会被选中,即使我没有选择它。

最后可能与之相关的是: 我在每一行中都进行了一些操作,具体取决于所选的女巫行,当我选择第一个时就可以了,但是当我取消选择并选择其他行时,第一个会在某处被记住,而我无法选择下一个,所以此时点没有选择任何内容,我不能再选择任何内容。

iOS 12 及更低版本一切正常。

我该如何解决这个问题?

这是我的代码的一部分:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //   return self.patientData.arrayNames.count
    return structuredNames.count;
}

// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:PatientExercizeCell = self.table.dequeueReusableCell(withIdentifier: "exercizeCell") as! PatientExercizeCell
    cell.cellIndex = indexPath.row

    cell.exercizeName.text = structuredNames[indexPath.row][1]
    cell.appName.text = structuredNames[indexPath.row][0]
    cell.dateOfExercize.text = structuredNames[indexPath.row][2] + " " + structuredNames[indexPath.row][3]

    if(!listOfNotes.isEmpty){
        if(listOfNotes[indexPath.row] != ""){
            cell.noteButton.isHidden = false
            cell.noteButton.tag = indexPath.row
        }else{

            cell.noteButton.isHidden = true
        }
    }

    if(fileHandler.listOfFilesFromDevice.contains(fileNames[indexPath.row]) && patientIsSended && !(leftoverFiles.contains(fileNames[indexPath.row].components(separatedBy: ".t")[0]))){
        cell.deleteButton.isHidden = false
        cell.deleteButton.tag = indexPath.row
    }else{
        cell.deleteButton.isHidden = true
    }

    if(GlobalSession.checkedExercises.contains(indexPath.row)){
        cell.checkMark.isOn = true
    }else{
        cell.checkMark.isOn = false
    }
    return cell
}


// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    indexPathNumber = indexPath.row
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
    selectedCell.contentView.backgroundColor = Color().blue()


}

Video about difference between ios 12 and 13

最佳答案

我认为这是因为细胞的循环行为

我的建议是

  1. 在 tableView(_:didDeselectRowAt:) 中不要更改背景颜色,只需将所选行添加到数组并重新加载数据
  2. 在 tableView(_:cellForRowAt:) 中根据数组选定的行设置背景颜色

关于swift - iOS 13 UITableView 选择/取消选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834862/

相关文章:

sql - SQL Server 2008下可以使用星号(*)来过滤WHERE子句中的列吗?

python - 崇高文本 3 API : Get all text from a file

ios - 从节点获取纹理名称 - Swift/Spritekit

ios - 为什么当我的基础数据源更新时 SwiftUI 会自动向后导航,我该如何避免这种行为?

ios - UITableViewCell 及其 socket 是否在 `dequeueReusableCellWithIdentifier` 之后初始化?

MYSQL基于多级产品的产品总销售额

swift - 将 NSPredicate/NSCompoundPredicate 与 Where 子句或可选返回值一起使用,CloudKit

ios - 在 Swift 3 中将结构写入 outputStream

cocoa-touch - UITableViewCell 作为 subview ?

ios - SWIFT - 基于选择的 UITableViewCell 更新