在tableview中选择多行并勾选选定的行

时间:2015-05-28 20:25:17

标签: swift

我从plist文件加载tableView。这没有问题。我只是想"打勾"选定的行。目前,凭借我的代码,它并没有按照预期的那样工作。目前,它看起来如下:

  • 点击row1(它会勾选第1行=好)
  • 再次点击row1(没有任何反应=糟糕。我希望此行未被删除) 在第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
        }

    }
}

5 个答案:

答案 0 :(得分:64)

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 / storyboard中继承原型单元

enter image description here

答案 1 :(得分:9)

首先,转到故事板并选择tableview,然后在“属性”检查器中,将选择设置为多项选择

Attributes Inspector with multiple selection

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

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

答案 2 :(得分:8)

这样可以解开。

INotifyPropertyChanged

答案 3 :(得分:3)

你必须制作一个服装类来获取你必须覆盖一个被调用的func的单元格的选定状态 setSelected(_ selected:Bool,animated: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
        }
}

答案 4 :(得分:2)

这个问题有很多解决方案,这是我提出的问题。我正在使用内置单元格“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
    }
}

我在这里制作了一个示例项目:https://github.com/brcimo/SwiftTableViewMultipleSelection

相关问题