ios - 单击多个单元格上的 UItableview 正在快速检查 3

标签 ios iphone swift xcode uitableview

每当我选择第 4 个单元格时,我在 uitableview 中遇到问题,没有自动选择单元格...我厌倦了这个问题,有人请帮助这里是我的 didselectrowfunc..

////////////////For Adding Check Box////////////////////////

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
    {
        if selectedDisease.count > 0
        {
            let no = selectedDisease.index(of: finall[indexPath.row])
            selectedDisease.remove(at: no!)
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
            print(selectedDisease)
        }
        else
        {
            selectedDisease = [""]
        }
    }
    else
    {
        if selectedDisease.contains("")
        {
            selectedDisease.removeAll()
            var name = finall[indexPath.row]
            selectedDisease.append(name)
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
            print(selectedDisease)
        }
        else
        {
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
            selectedDisease.append(finall[indexPath.row])
            //selectedDisease = [finall[indexPath.row]]
            print(selectedDisease)
        }
    }
}

/////////CellForRowAt///////////////

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "diseasetable", for: indexPath) as! FoodTableViewCell

    cell.DiseaseName.text = finall[indexPath.row]
    //indexsave.append(indexPath.row)

    return cell
}

最佳答案

试试这个逻辑希望这能解决你的问题

    let dict = [NSIndexPath: String]()

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

         if let _ = // dict contains value at indexpath {
             cell.accessoryType = .Checkmark
         } else {
             cell.accessoryType = .None
         }

         return cell
     }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          if ( // dict contains value == indexpath) {
              // remove value from dict
          } else {
          //add value to dict
          }
          tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
      }

关于ios - 单击多个单元格上的 UItableview 正在快速检查 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43406171/

相关文章:

iphone - UIToolBar 图像显示不正确

ios - Advanced NSOperation - 在运行时添加依赖

iphone - 从捕获的视频帧创建 openGL 纹理以在视频上覆盖 openGL View 的替代方案? (iPhone)

iphone - 制作我自己的 UIControl 事件并触发它?

ios - UIColor 不适用于 RGBA 值

ios - MagicalRecord 保存不会持续存在,直到应用程序第一次退出并重新运行

iphone - UISearchbar 键盘搜索按钮操作

ios - 如果我们可以使用 segues 返回到任何 View Controller ,为什么我们还要使用 unwind segue?

arrays - 如何制作一个函数,将协议(protocol)中定义的任何函数作为其参数?

ios - TableView 单元格不可转换为 Void