简体   繁体   English

单个单元格未发生TableView单元格扩展

[英]TableView cell expansion not happening for single cell

I am expanding a tableViewCell by using the below.However the problem is I am able to expand multiple cells at a time.What I want to do is , if one cell is expanded and any other cell is clicked, the previous one should be unexpanded thus one cell at a time. 我正在使用以下方法来扩展tableViewCell 。但是问题是我能够一次扩展多个单元格。我想做的是,如果一个单元格被扩展并且单击了其他任何单元格,则前一个单元格应该不扩展因此一次只有一个单元。

Here is how I am expanding. 这就是我的扩展方式。

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


if ([self.expandedCells containsObject:indexPath]) {


    [self.expandedCells removeObject:indexPath];



}else{
    isExpanded=YES;
    [self.expandedCells addObject:indexPath];
    //Populate expanded cell here

 }

 [self.tableView beginUpdates];
 [self.tableView reloadData];
 [self.tableView endUpdates];


}

Got it.Just had to check array count before adding anything. 知道了,只需要在添加任何东西之前检查数组计数即可。

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


  if ([self.expandedCells containsObject:indexPath]) {


  [self.expandedCells removeObject:indexPath];



  }else{
   isExpanded=YES;
   if (self.expandedCells.count>0) {
   [self.expandedCells removeAllObjects];
    }


  [self.expandedCells addObject:indexPath];
  //Populate expanded cell here

}

 [self.tableView beginUpdates];
 [self.tableView reloadData];
 [self.tableView endUpdates];


}

I am assuming that self.expandedCells has the array of cells to be expanded. 我假设self.expandedCells具有要扩展的单元格数组。 If thats the case, in didSelctRow, remove all objects from the expandedCells array and then add the selected cell. 如果是这样,请在didSelctRow中,从expandedCells数组中删除所有对象,然后添加选定的单元格。 And perform reload. 并执行重新加载。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    [self.expandedCells removeAllObjects];
    isExpanded=YES;
    [self.expandedCells addObject:indexPath];



     [self.tableView beginUpdates];
     [self.tableView reloadData];
     [self.tableView endUpdates];


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM