gpt4 book ai didi

ios - swift3 中 Tableview 中的多个复选标记

转载 作者:可可西里 更新时间:2023-11-01 01:21:50 24 4
gpt4 key购买 nike

如何在 tableview 中做多个复选标记。我需要在 tableview 中选择多个复选标记,我需要选择哪些复选标记来将多个值放在标签中。标签中的示例 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()
}

最佳答案

更改您的 selectedindex 以保存索引路径数组 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)!))!)
}
}

关于ios - swift3 中 Tableview 中的多个复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43464154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com