TableView

时间:2018-03-16 08:25:42

标签: ios swift xcode 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
}

}

4 个答案:

答案 0 :(得分:2)

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

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

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

<强>更新

您甚至可以像这样使用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)
}

答案 1 :(得分:1)

滚动时使bool数组稳定,即

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
}
希望这有帮助!

答案 2 :(得分:0)

对于多重选择,您应该跟踪Dictionary中的选定单元格,以便更快地访问选定和未选定的indexPath,允许您使用多个部分,因为我们的Dictionary的键值是由(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
    }

<强>结果

enter image description here

答案 3 :(得分:0)

只是一个小小的改变

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

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