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

tef*_*ter 22 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
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_* G. 74

斯威夫特4

首先,让你的tableView支持多种选择:

self.tableView.allowsMultipleSelection = true
self.tableView.allowsMultipleSelectionDuringEditing = true
Run Code Online (Sandbox Code Playgroud)

然后简单地将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
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,在你的身上使用它cellForRowAt indexPath:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
    for: indexPath) as? CheckableTableViewCell
Run Code Online (Sandbox Code Playgroud)

如果必须,请不要忘记在xib/storyboard中继承原型单元格:

在此输入图像描述


小智 11

首先,转到Storyboard并选择tableview,然后在Attributes Inspector中,将Selection设置为Multiple Selection.

属性Inspector具有多个选择

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

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


小智 8

这样可以解开.

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
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您必须制作服装类以获取单元格的选定状态,在该状态下您必须重写名为setSelected(_ selected:Bool,animation: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
        }
}
Run Code Online (Sandbox Code Playgroud)


Bry*_*imo 3

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 这似乎不起作用。`tableView:didSelectRowAtIndexPath:` 仅在选择行时调用,而不是在取消选择时调用,因此 `cell.selected` 将始终为 true! (2认同)