在swift3中的Tableview中有多个复选标记

时间:2017-04-18 05:19:10

标签: ios swift uitableview

如何在tableview中执行多重复选标记。我需要在tableview中选择多个复选标记,我需要选择哪些复选标记来将多个值放在label中。 标签

中的示例player1,player2,player3

这是我的代码

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return TypeOfAccountArray.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell


        let cell:TypeofAccountCell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TypeofAccountCell

        cell.Uertype_lbl.text=TypeOfAccountArray[indexPath.row]

        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        cell.Uertype_lbl.font = UIFont(name:"Roboto-Regular", size:13)
        cell.Uertype_lbl.adjustsFontSizeToFitWidth = true


        if (selectedIndex == indexPath as NSIndexPath?) {
            cell.checkmarkbtn.setImage(UIImage(named: "checkmark.png"),for:UIControlState.normal)
        } else {
            cell.checkmarkbtn.setImage(UIImage(named: "uncheckmark.png"),for:UIControlState.normal)
        }


        // Configure the cell...

        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        let row = indexPath.row
        print(TypeOfAccountArray[row])
        selectedIndex = indexPath as NSIndexPath?
        self.Type_of_account_txt.text = (TypeOfAccountArray[row])
        self.Type_account_view.isHidden = true
        tableView.reloadData()
    }

2 个答案:

答案 0 :(得分:2)

将您选择的索引更改为保存索引路径var selectedIndexes = [IndexPath]()的数组,在单元格xib上,在选中的按钮上设置复选标记图像,并在正常状态下取消选中图像,然后使用以下代码。

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return TypeOfAccountArray.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:TypeofAccountCell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TypeofAccountCell

        cell.Uertype_lbl.text=TypeOfAccountArray[indexPath.row]

        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        cell.Uertype_lbl.font = UIFont(name:"Roboto-Regular", size:13)
        cell.Uertype_lbl.adjustsFontSizeToFitWidth = true



        // Configure the cell...

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)



        let cell:TypeofAccountCell=tableView.cellForRow(at: indexPath) as! TypeofAccountCell

        if selectedIndexes.contains(indexPath)
        {

            cell.checkmarkbtn.isSelected = false

            if let index = selectedIndexes.index(of: indexPath) {
                selectedIndexes.remove(at: index)
            }
        }
        else
        {
            cell.checkmarkbtn.isSelected = true

            selectedIndexes.append(indexPath)

        }
    }



    self.Type_of_account_txt.text = ""
    for element in selectedIndexes
    {
        self.Type_of_account_txt.text = (self.Type_of_account_txt.text ?? "") + "\(TypeOfAccountArray[element.row]) ,"


    }
    if (selectedIndexes.count > 0)
    {
        self.Type_of_account_txt.text = self.Type_of_account_txt.text?.substring(to: (self.Type_of_account_txt.text?.index(before: (self.Type_of_account_txt.text?.endIndex)!))!)
    }
}

答案 1 :(得分:0)

您需要按照以下步骤操作:

  1. 在didSelectRowAt中,您需要在数组中添加和删除indexpath以获取多个复选标记。

  2. 现在,在cellForRowAtIndexPath中,您需要检查当前的

    indexPath包含在数组中。

     if (![arrIndexPath containsObject: indexPath]) {
    
            //  do something
    
        cell.checkmarkbtn.setImage(UIImage(named: "checkmark.png"),for:UIControlState.normal)
    
                 }