Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Tableview单元格未根据选择选择行操作_Ios_Swift_Uitableview_Uibutton - Fatal编程技术网

Ios Tableview单元格未根据选择选择行操作

Ios Tableview单元格未根据选择选择行操作,ios,swift,uitableview,uibutton,Ios,Swift,Uitableview,Uibutton,我有一些问题和答案 将问题放入一个数组,将答案放入一个数组 我想在用户选择问题时显示再次显示答案选择关闭答案 我写下面的代码,但当一个答案是开放的,仍然是所有的关闭,我的逻辑是反向任何一个plz帮助我 在这里,我创建了一个带有selectedindex名称的全局变量 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = t

我有一些问题和答案

将问题放入一个数组,将答案放入一个数组

我想在用户选择问题时显示再次显示答案选择关闭答案

我写下面的代码,但当一个答案是开放的,仍然是所有的关闭,我的逻辑是反向任何一个plz帮助我

在这里,我创建了一个带有selectedindex名称的全局变量

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