Ios Tableview单元格未根据选择选择行操作
我有一些问题和答案 将问题放入一个数组,将答案放入一个数组 我想在用户选择问题时显示再次显示答案选择关闭答案 我写下面的代码,但当一个答案是开放的,仍然是所有的关闭,我的逻辑是反向任何一个plz帮助我 在这里,我创建了一个带有selectedindex名称的全局变量Ios Tableview单元格未根据选择选择行操作,ios,swift,uitableview,uibutton,Ios,Swift,Uitableview,Uibutton,我有一些问题和答案 将问题放入一个数组,将答案放入一个数组 我想在用户选择问题时显示再次显示答案选择关闭答案 我写下面的代码,但当一个答案是开放的,仍然是所有的关闭,我的逻辑是反向任何一个plz帮助我 在这里,我创建了一个带有selectedindex名称的全局变量 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = t
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DonationTableViewCell", for: indexPath)as!
DonationTableViewCell
cell.questioncell.text = questnArr[indexPath.row]
if indexPath.row == selectedindex
{
cell.answerlbl.text = answersarr[indexPath.row]
cell.questioncell.textColor = UIColor.disSatifyclr
cell.questionimg.image = #imageLiteral(resourceName: "Drop down top icon")
}else{
cell.answerlbl.text = ""
cell.questioncell.textColor = UIColor.textmaincolor
cell.questionimg.image = #imageLiteral(resourceName: "Drop down")
}
tableviewheight.constant = tableview.contentSize.height
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedindex = indexPath.row
tableView.reloadData()
}
首先,将
selectedindex
声明为可选indepath
var selectedIndexPath : IndexPath?
在didSelectRowAt
中,您必须执行一些检查:
- 如果
选择selectedIndexPath==nil
indexPath
- 如果
和选择了索引路径!=nil
取消选择selectedIndexPath==indexPath
indexPath
- 如果
和选择了索引路径!=无
取消选择选择的索引路径!=indexPath
处的行,然后选择selectedIndexPath
处的行indexPath
在
cellForRowAt中检查
if indexPath == selectedIndexPath
不清楚你在问什么。您的问题是否与行高有关?如果是,请显示您的heightForRowAt
。在cellForRowAt
中的tableviewheight.constant=tableview.contentSize.height
行有什么意义?这不应该存在。
if indexPath == selectedIndexPath