【问题标题】:Issue with multiple selection and select all in UITableview in swift多选问题并在 UITableview 中快速全选
【发布时间】:2018-10-30 18:18:39
【问题描述】:

在我的应用程序中,我有一个多选和全选选项,我已经尝试使用下面的代码,但面临选择问题。

如果我按 selectAll,它会清楚地检查和取消检查,如果假设,我选择它选择的单个选择,但所选单元格没有突出显示。

谁能帮我弄清楚。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return self.array.count
}

在 cellForRowAtIndexPath 中,

let cell = self.multipleSelectionTableview.dequeueReusableCell(withIdentifier:multiSelectionIdentifier , for: indexPath) as! MultipleSelectionCell
        if self.selectedRows.contains(indexPath){
            cell.checkBoxBtn.isSelected = true
            cell.checkBoxBtn.setImage(UIImage.init(named: "check"), for: .normal)

        } else {
            cell.checkBoxBtn.isSelected = false
            cell.checkBoxBtn.setImage(UIImage.init(named: "uncheck"), for: .normal)
        }


        if self.multipleSelectionStatusBtn.isSelected == true {
            self.multipleSelectionStatusBtn.isSelected = true
            cell.checkBoxBtn.setImage(UIImage.init(named: "check"), for: .normal)
        } else {
            self.multipleSelectionStatusBtn.isSelected = false
            cell.checkBoxBtn.setImage(UIImage.init(named: "uncheck"), for: .normal)
        }

        cell.checkBoxBtn.tag = indexPath.row
        return cell

在复选框选择方法中,

let selectedIndexPath = IndexPath(row: sender.tag, section: 0)
        self.multipleSelectionTableview.deselectRow(at: selectedIndexPath, animated: true)
        if self.selectedRows.contains(selectedIndexPath)
        {

            self.selectedRows.remove(at: self.selectedRows.index(of: selectedIndexPath)!)
        }
        else
        {

            self.selectedRows.append(selectedIndexPath)
            print(self.selectedRows)

        }

        self.multipleSelectionTableview.reloadData()

在 selectAll 方法中,

if (sender.isSelected == true)
        {

            sender.setImage(UIImage(named: "uncheck"), for: .normal)
            sender.isSelected = false;
           // isSelecting = false
            self.selectedRows = self.getAllIndexpaths()
            self.multipleSelectionTableview.reloadData()

        }
        else
        {
           // isSelecting = true
            sender.setImage(UIImage(named: "check"), for: .normal)
            sender.isSelected = true;
            self.selectedRows = self.getAllIndexpaths()
            self.multipleSelectionTableview.reloadData()
        }

对于getAllIndexPaths方法,

func getAllIndexpaths() -> [IndexPath] {
        var indexPaths: [IndexPath] = []
        for j in 0..<self.multipleSelectionTableview.numberOfRows(inSection: 0) {
            indexPaths.append(IndexPath(row: j, section: 0))
        }
        return indexPaths
    }

【问题讨论】:

  • 你可以实现willSelectRowAt:的tableview委托方法并改变单元格背景颜色,
  • 检查你的 cellForRowAtIndexPath 一次,你验证了多个条件
  • 确保已设置 tableView.allowsMultipleSelection = true
  • @Anbu.karthik 在单选后不会调用该单元格,但所选状态将附加到所选行的数组中。
  • @HAK 已启用多选状态。

标签: ios swift uitableview multipleselection selectall


【解决方案1】:

实际上,当复选框被选中或取消选中时,您正在重新加载数据。这就是表格视图不保留选择的原因。

解决方案。

您需要跟踪选定的索引路径。在cellForRowAt: 中,您需要检查它是否是选中的索引路径,您需要通过更改其背景颜色来突出显示单元格,否则将单元格背景颜色设置为白色或其他颜色。

例如。

//In cellForRowAtIndexPath

 if arrSelectedIndexPath.contains(indexPath) {
     // checkbox is checked, change cell bg color
 } else {
    // checkbox is not checked
 }

arrSelectedIndexPath 是一个[IndexPath] 一个索引路径数组。当复选框被选中时,您需要在数组中插入索引路径,当复选框未选中时,您需要从数组中删除该索引路径。

【讨论】:

  • var selectedRows : [IndexPath] = [],我只是这样使用
  • 您可以检查selectedRows是否包含选定的索引路径包含或不包含,您可以在此基础上突出显示单元格。
  • 单选后不调用cellForRowAtIndexpath。在多选中调用,
【解决方案2】:

我对@9​​87654321@ 中的逻辑实现感到困惑:

首先您要检查selectedRows 数组是否包含选定的index path 并设置checkBoxBtn 的UI:

let cell = self.multipleSelectionTableview.dequeueReusableCell(withIdentifier:multiSelectionIdentifier , for: indexPath) as! MultipleSelectionCell
if self.selectedRows.contains(indexPath){
    cell.checkBoxBtn.isSelected = true
    cell.checkBoxBtn.setImage(UIImage.init(named: "check"), for: .normal)

} else {
    cell.checkBoxBtn.isSelected = false
    cell.checkBoxBtn.setImage(UIImage.init(named: "uncheck[enter image description here][1]"), for: .normal)
}

然后您再次检查multipleSelectionStatusBtn 并更改checkBoxBtn 的用户界面:

if self.multipleSelectionStatusBtn.isSelected == true {
    self.multipleSelectionStatusBtn.isSelected = true
    cell.checkBoxBtn.setImage(UIImage.init(named: "check_timesheet"), for: .normal)
} else {
    self.multipleSelectionStatusBtn.isSelected = false
    cell.checkBoxBtn.setImage(UIImage.init(named: "uncheck_timesheet"), for: .normal)
}

cell.checkBoxBtn.tag = indexPath.row
return cell

tableView 显然会重新加载时,这将适用于多选而不是单选。

改变

selectAll 方法中:

if (sender.isSelected == true)
{

    sender.setImage(UIImage(named: "uncheck"), for: .normal)
    sender.isSelected = false;
   // isSelecting = false
    self.selectedRows.removeAll()
    self.multipleSelectionTableview.reloadData()

}
else
{
   // isSelecting = true
    sender.setImage(UIImage(named: "check"), for: .normal)
    sender.isSelected = true;
    self.selectedRows = self.getAllIndexpaths()
    self.multipleSelectionTableview.reloadData()
}

cellForRowAtIndexPath

let cell = self.multipleSelectionTableview.dequeueReusableCell(withIdentifier:multiSelectionIdentifier , for: indexPath) as! MultipleSelectionCell
if self.selectedRows.contains(indexPath){
    cell.checkBoxBtn.isSelected = true
    cell.checkBoxBtn.setImage(UIImage.init(named: "check"), for: .normal)

} else {
    cell.checkBoxBtn.isSelected = false
    cell.checkBoxBtn.setImage(UIImage.init(named: "uncheck[enter image description here][1]"), for: .normal)
}
if getTimeSheetJSON[indexPath.row]["TaskName"].string == "" {
    cell.checkBoxBtn.isHidden = true
} else {
    cell.checkBoxBtn.isHidden = false
}

cell.checkBoxBtn.tag = indexPath.row
return cell

希望这可行,因为我还没有在 Xcode 中编译您的代码。

【讨论】:

  • 更改了“在 selectAll 方法中”逻辑。请立即检查。
  • 您已经保存在selectedRows array 对...??
【解决方案3】:

您必须将选定的索引路径存储在数组中,这样您就可以轻松做到这一点。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var selectedRow:[IndexPath] = []
@IBOutlet var tblView:UITableView!

override func viewDidLoad()
{
    super.viewDidLoad()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section:
    Int) -> Int
{
    return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

    if cell == nil
    {
        cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell")
    }

    cell?.textLabel?.text = String(format:"%@",indexPath.row)

    if selectedRow.contains(indexPath)
    {
        cell?.accessoryType = .checkmark
    }
    else
    {
        cell?.accessoryType = .none
    }        
    return cell!
}
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if selectedRow.contains(indexPath)
    {
        // For  Remove Row selection
        selectedRow.remove(at: selectedRow.index(of: indexPath)!)
    }
    else
    {
        selectedRow.append(indexPath)
    }

    tableView.reloadData()
}

// Call this method for Select All
func selectAllCall()
{
    var indexPaths: [IndexPath] = []
    for j in 0..<100
    {
        indexPaths.append(IndexPath(row: j, section: 0))
    }

    selectedRow.removeAll()

    selectedRow.append(contentsOf: indexPaths)

    tblView.reloadData()
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2017-07-28
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多