繁体   English   中英

TableView上的多个单元格选择

[英]Multiple Cell Selection on TableView

我遇到一个问题,当我为索引3选择单元格时,它也在随机索引下面选择单元格。 “检查和取消选中”单元格正在工作,但是由于某些原因,在选择一个单元格时,它也在选择其他单元格。 我的数组总共返回120行。 我选择了多点触控。 感谢您的帮助。

 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
}

}

您应该喜欢这种方式,如果只有一个部分,这是非常简单的解决方案。

像这样初始化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)
}

代码将根据您的要求和自定义单元进行更改。 希望你能做到。 谢谢。

UPDATE

您甚至可以像这样使用Set

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

使布尔数组在滚动时保持稳定性,即

var arrStatusBool = [Bool]()

现在在didSelectRowAt中的indexPath.row处设置值

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    if self.arrStatusBool[indexPath.row]
    {
        self.arrStatusBool[indexPath.row] = false
    } 
    else 
    {
        self.arrStatusBool[indexPath.row] = true
    }
}

并将其放在cellForRowAt中以避免滚动问题。

if self.arrStatusBool[indexPath.row]
{
     tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
} 
else 
{
    tableView.cellForRow(at: indexPath)?.accessoryType = .none
}

希望对您有所帮助!

对于多选,您应该跟踪“ Dictionary选定单元格以便于快速访问选定和未选择的indexPath,从而允许您使用多个部分,因为“字典”的键值是由(IndexPath.section)+(IndexPath.row)组成的字符串。总是独特的组合

var selectedIndexPaths : [String:Bool] = [:]

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

    let currentIndexPathStr = "\(indexPath.section)\(indexPath.row)"
    if(self.selectedIndexPaths[currentIndexPathStr] == nil || !self.selectedIndexPaths[currentIndexPathStr]!) {
        self.selectedIndexPaths[currentIndexPathStr] = true
    }else{
        self.selectedIndexPaths[currentIndexPathStr] = false
    }
    self.tableView.reloadRows(at: [indexPath], with: .automatic)
}


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

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

        cell.mTitle.text = musics[indexPath.row]["title"] as! String?
        cell.mArtist.text = musics[indexPath.row]["artist"] as! String?

        cell.accessoryType = .checkmark
        let currentIndexPathStr = "\(indexPath.section)\(indexPath.row)"
        if(self.selectedIndexPaths[currentIndexPathStr] == nil || !self.selectedIndexPaths[currentIndexPathStr]!)  {
           cell.accessoryType = .none
        }

        return cell
    }

结果

在此处输入图片说明

只是一个小小的改变

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.accessoryType = UITableViewCellAccessoryType.checkmark
    }
    else
    {
       cell.accessoryType = UITableViewCellAccessoryType.none
    }

    return cell
}

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

    let cell = tableView.cellForRow(at: indexPath)

    //toggle the state
    cell!.isSelected = !cell!.isSelected

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

注意:您还应该为通用代码创建方法:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM