Swift 在tableview中选择多行并勾选所选行

Swift 在tableview中选择多行并勾选所选行,swift,Swift,我正在从plist文件加载tableView。这是没有问题的。我只想“勾选”所选行。目前,使用我的代码,它没有按预期工作。目前情况如下: 轻触第1行(它将勾选第1行=良好) 再次点击第1行(无任何变化=不好。我希望在这里该行被取消勾选) 在第1行上再次单击时,它会松开。在第二次点击它之后 当我在tableview的初始加载时点击row0时,它不会勾选该行 我的代码: class portals: UITableViewController { var lastSelectedInd

我正在从plist文件加载tableView。这是没有问题的。我只想“勾选”所选行。目前,使用我的代码,它没有按预期工作。目前情况如下:

  • 轻触第1行(它将勾选第1行=良好)
  • 再次点击第1行(无任何变化=不好。我希望在这里该行被取消勾选) 在第1行上再次单击时,它会松开。在第二次点击它之后
  • 当我在tableview的初始加载时点击row0时,它不会勾选该行
我的代码:

class portals: UITableViewController {

    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)

...

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

        // Configure the cell...
        cell.textLabel!.text = portals[indexPath.row]

        return cell
    }


    // Check which portal is selected
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var whichPortalIsSelected: String = ""

        // Get Cell Label
        let indexPath = tableView.indexPathForSelectedRow();

        // Tick the selected row
        if indexPath!.row != lastSelectedIndexPath?.row {

            let newCell = tableView.cellForRowAtIndexPath(indexPath!)
            newCell?.accessoryType = .Checkmark

            lastSelectedIndexPath = indexPath

            whichPortalIsSelected = newCell!.textLabel!.text!
            println("You selected cell #\(lastSelectedIndexPath.row)!") //PPP
            println("You selected portal #\(whichPortalIsSelected)!") //PPP

        // Un-Tick unselected row
        } else {
            let newCell = tableView.cellForRowAtIndexPath(indexPath!)
            newCell?.accessoryType = .None

            whichPortalIsSelected = newCell!.textLabel!.text!
            println("You unselected cell #\(indexPath!.row)!") //PPP
            println("You unselected portal #\(whichPortalIsSelected)!") //PPP
        }

    }
}

这个问题有很多解决方案,我想出了一个。我正在使用内置单元格“selected”属性,因此tableview会为我们保存它。只需确保在故事板中或在代码中设置tableview时使用多个选择

import UIKit

class TableViewController: UITableViewController
{
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)

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

        // Configure the cell...
        cell.textLabel!.text = "row: \(indexPath.row)"

        if cell.selected
        {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryType.None
        }

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let cell = tableView.cellForRowAtIndexPath(indexPath)

        if cell!.selected == true
        {
            cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        else
        {
            cell!.accessoryType = UITableViewCellAccessoryType.None
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 100
    }
}
我在这里做了一个示例项目:

这将启用untick

class TableViewController: UITableViewController
{
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)

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

        // Configure the cell...
        cell.textLabel!.text = "row: \(indexPath.row)"

        if cell.selected
        {
            cell.selected = false
            if cell.accessoryType == UITableViewCellAccessoryType.None
            {
                cell.accessoryType = UITableViewCellAccessoryType.Checkmark
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryType.None
            }
        }

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let cell = tableView.cellForRowAtIndexPath(indexPath)

        if cell!.selected
        {
            cell!.selected = false
            if cell!.accessoryType == UITableViewCellAccessoryType.None
            {
                cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
            }
            else
            {
                cell!.accessoryType = UITableViewCellAccessoryType.None
            }
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 100
    }
}

Swift 4

首先,使tableView支持多种选择:

self.tableView.allowsMultipleSelection = true
self.tableView.allowsMultipleSelectionDuringEditing = true
然后简单地将UITableViewCell子类化如下:

class CheckableTableViewCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
    }
}
最后,在您的
cellForRowAt indexPath
中使用它,如下所示:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
    for: indexPath) as? CheckableTableViewCell
如果必须,请不要忘记在xib/情节提要中对原型单元进行子分类:

您必须创建一个costude类来获取单元格的选定状态,在该状态下,您必须重写一个名为 setSelected(uSelected:Bool,动画:Bool) 或者,当您滚动时,勾号将随机显示。。。 下面是我所做工作的一个例子: 1-为单元格创建了一个类 2-为图像添加了一个显示勾号的插座(如果您不想要服装勾号图像,可以跳过此选项) 3-重写函数并使用所选参数:D

这是我的班级:

import UIKit

class AddLocationCell: UITableViewCell {

     @IBOutlet weak var check: UIImageView!

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

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            if selected{
                check.image = UIImage(named:"check_active")
            }else{
                check.image = UIImage(named:"check_normal")

            }
            // Configure the view for the selected state
        }
}

首先,转到情节提要并选择tableview,然后在属性检查器中,将选择设置为多选

然后,覆盖UITableViewCell子类中的setSelected(uSelected:Bool,animated:Bool)函数

override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        accessoryType = selected ? UITableViewCellAccessoryType.checkmark : UITableViewCellAccessoryType.none
    }

这似乎不起作用<代码>表格视图:didSelectRowAtIndexPath:仅在选中行时调用,而不是在取消选择时调用,因此
单元格。selected
将始终为真!这太完美了。谢谢。这种方法非常有用。请告诉我如何在表视图中获取所选单元格的索引,或者更好地指导我,如果我有关于单元格的更多信息数组,如何获取所选单元格的信息数组而不是全部不幸的是,在滚动表视图时,使用可重用的表视图单元格时,所选单元格的
isSelected
属性似乎丢失。因此,您可以选择多个单元格,选中复选标记,在tableView中向下滚动,再次向上滚动,单元格将再次取消选中。有什么想法吗?你是对的@Alienbash,我相应地修改了我的答案。如果没有@Aatish Rajkarnikar answer,这个答案是无效的,在这里你需要将你的表格视图从选择设置为多个选择。谢谢你的提醒@njais这不是设置所选标记而不是cellForRowAt和didSelectRowAt的最好或正确的方法吗?