2017-09-25 82 views
0

我提出的扩大tableViewCell和我的变量而言的默认值。如果该值为然后细胞收缩,当其真正然后细胞扩大。问题是,当一个细胞的值我希望所有其他细胞值为,我很努力。这里是我下面的代码:取消选择的行取决于值时的UITableView选择其他行夫特

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

    let content = datasource[indexPath.row] 

    //Here content.expand = false by default in other class 

    if indexPath.row == 0 { 
     print("0 row") 
     content.expanded = !content.expanded 

    } else if indexPath.row == 1 { 
     print("1 row") 
     content.expanded = !content.expanded 

    } else if indexPath.row == 2 { 
     print("2 row") 
     content.expanded = !content.expanded 
    } 

    tableView.reloadRows(at: [indexPath], with: .automatic) 

} 

以下情形的图片: enter image description here

我不想要两个细胞在同一时间进行扩展。请帮忙。

回答

2

首先声明这个变量

var selectedIndex: IndexPath? 

接下来,这里就是你的选择代码:

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


    if(selectedIndex != indexPath){ // It's not the expanded cell 

     var indicesArray = [IndexPath]() 

     if(selectedIndex != nil){ //User already expanded other previous cell 

      let prevExpandedContent = datasource[(selectedIndex?.row)!] 

      prevExpandedContent.expanded = !prevExpandedContent.expanded 

      indicesArray.append(selectedIndex!) 

     } 

     // Assign new expanded cell 

     selectedIndex = indexPath 


     let currentExpandedContent = datasource[indexPath.row] 

     currentExpandedContent.expanded = !currentExpandedContent.expanded 


     indicesArray.append(indexPath) 

     tableView.reloadRows(at: indicesArray , with: .automatic) 



    } 



} 

希望这将简化您的要求。

相关问题