TableView 上的多个单元格选择 [英] Multiple Cell Selection on TableView

查看:25
本文介绍了TableView 上的多个单元格选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,当我选择单元格时,例如在索引 3 处,它也在随机索引处选择下面的单元格.选中和取消选中单元格正在工作,但由于某些原因,在选择一个单元格时,它也会选择其他单元格.我的数组总共返回 120 行.我选择了多点触控.谢谢你的帮助.

I'm having an issue where when I'm selecting the cell for e.g at index 3 , it selecting cells below also at random indexes. Check and Uncheck cell is working but for some reasons when selecting a cell it is selecting other cells as well. My array is returning 120 rows in total. I have selected multiple touch. Thank you for the help.

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return arrayVerbCount.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MesTalentsChoixCell

    cell.verb!.text = arrayVerbCount[indexPath.row].verb

    if cell.isSelected
    {
        cell.isSelected = false
        if cell.accessoryType == UITableViewCellAccessoryType.none
        {
            cell.accessoryType = UITableViewCellAccessoryType.checkmark
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryType.none
        }
    }

    return cell
}

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

    let cell = tableView.cellForRow(at: indexPath)

    if cell!.isSelected
    {
        cell!.isSelected = false
        if cell!.accessoryType == UITableViewCellAccessoryType.none
        {
            cell!.accessoryType = UITableViewCellAccessoryType.checkmark
        }
        else
        {
            cell!.accessoryType = UITableViewCellAccessoryType.none
        }
    }
}

我的自定义单元类:

class MesTalentsChoixCell: UITableViewCell {

@IBOutlet weak var verb: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

推荐答案

你应该这样做,如果只有一个部分,这是非常简单的解决方案.

You should do like this way, this is very much easy solution if there is only one section.

像这样初始化selectedItems数组,

var selectedItems: [Int] = []

在下面找到UITableViewDataSource方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
    cell.tmpValue.text = data[indexPath.row]

    if selectedItems.contains(indexPath.row) {
        cell.accessoryType = .checkmark
    } else {
        cell.accessoryType = .none
    }

    return cell
}

在下面找到UITableViewDelegate方法.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    if selectedItems.contains(indexPath.row) {
        let index = selectedItems.index(of: indexPath.row)
        selectedItems.remove(at: index!)
    } else {
        selectedItems.append(indexPath.row)
    }

    tableView.reloadRows(at: [indexPath], with: .none)
}

代码将根据您的要求和自定义单元格进行更改.希望你能按照自己的方式去做.谢谢你.

Code will be changed depending on your requirement and custom cell. Hope you can do it your way. Thank you.

更新

你甚至可以像这样使用Set

You can even use Set also like this way,

var setSelectedItems: Set<Int> = []

UITableViewDataSource 方法,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
    cell.tmpValue.text = data[indexPath.row]

    if setSelectedItems.contains(indexPath.row) {
        cell.accessoryType = .checkmark
    } else {
        cell.accessoryType = .none
    }

    return cell
}

UITableViewDelegate 方法,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    if setSelectedItems.contains(indexPath.row) {
        setSelectedItems.remove(indexPath.row)
    } else {
        setSelectedItems.insert(indexPath.row)
    }
    tableView.reloadRows(at: [indexPath], with: .none)
}

这篇关于TableView 上的多个单元格选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆