选择多个选项并在swift中的UITableview中选择全部

时间:2018-05-21 06:11:51

标签: ios swift uitableview multipleselection selectall

在我的应用程序中,我有多个选项并选择所有选项,我尝试过使用下面的代码,但我面临选择问题。

如果我按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
    }

3 个答案:

答案 0 :(得分:0)

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

解。

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

例如。

//In cellForRowAtIndexPath

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

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

答案 1 :(得分:0)

我对cellForRowAtIndexpath中的逻辑实现感到困惑:

首先,您要检查selectedRows数组是否包含选定的index path并设置了checkBoxBtn的用户界面:

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中编译您的代码。

答案 2 :(得分:0)

您必须在Array中存储选定的索引路径。这样您就可以轻松完成此操作。

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