gpt4 book ai didi

ios - 如何选中 swift4 单元格数据中的 TableView 单元格数据来自服务器

转载 作者:行者123 更新时间:2023-11-29 05:37:28 26 4
gpt4 key购买 nike

正如我在某些情况下尝试过的那样,但如果我选择第二个单元格,则不能完美工作,第一个单元格中的复选标记将被取消选中,有时,直到我单击 10 到 20 次后,该功能才根本无法工作。这是我的代码。

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

if tableView == switchTableView{
return self.arrdata20.count
} else
{
return self.arrdata.count
}

}


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

if (tableView == self.switchTableView)
{

let cell:switchTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! switchTableViewCell

cell.nameLbl.text = (arrdata20[indexPath.row].name)
print(cell.nameLbl.text)
if (arrdata20[indexPath.row].emp_id == "001")
{
cell.isHidden=true
}
else{
cell.isHidden=false
}
return cell



}
else {
let cell:PartyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PartyTableViewCell
cell.venuLbl.text = "Venu: \(arrdata[indexPath.row].place)"
cell.dateTimeLbl.text = "Date & Time: \(arrdata[indexPath.row].date)"
cell.reasonLbl.text = "Reason: \(arrdata[indexPath.row].reason)"
// cell.timeLbl.text = ""
return cell
}

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var rowHeight:CGFloat = 0.0
if tableView == self.switchTableView{
if(arrdata20[indexPath.row].emp_id == "001")
{
rowHeight = 0.0
}
else
{
rowHeight = UITableViewAutomaticDimension
}

return rowHeight
}else{
return UITableViewAutomaticDimension
}

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


id1 = "\(arrdata[indexPath.row].id)"
print(id1)

if self.switchTableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
self.switchTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none

}
else{
self.switchTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark



}
}

选择 tableView 单元格后,我需要获取复选标记单元格详细信息,例如单元格中的名称,如下图所示 enter image description here

最佳答案

您必须将数组声明为:

var checked = [Bool]()

然后在 API 调用中添加这行代码,您可以在 arraydata 中接收数据

self.checked = Array(repeating: false, count: self.arraydata.count)

在 TableView 委托(delegate)方法中:

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

let cell:switchTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! switchTableViewCell

//configure you cell here.
if checked[indexPath.row] == false{
cell.accessoryType = .none


} else if checked[indexPath.row] {
cell.accessoryType = .checkmark


}
cell.title.text = self. arraydata[indexPath.row]["amp_id"].string
return cell

}

添加另一个委托(delegate)方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {

if cell.accessoryType == .checkmark {
cell.accessoryType = .none
checked[indexPath.row] = false


} else {
cell.accessoryType = .checkmark
checked[indexPath.row] = true


}
}

on OKClickedButtonAction:

serviceString = ""
for i in 0..<checked.count{
if checked[i] == true{
serviceString = serviceString + self.arraydata[i]["emp_id"].string! + ", "
print(serviceString)

}
}
if serviceString == ""{
self.servicesBtnLbl.text = "Tap to select"
}else{
self.servicesBtnLbl.text = serviceString

}

这是可行的解决方案,希望对您有所帮助。

关于ios - 如何选中 swift4 单元格数据中的 TableView 单元格数据来自服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899500/

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