iOS从cellForRowAtIndexPath方法获取当前和以前选择的UITableViewCells

iOS从cellForRowAtIndexPath方法获取当前和以前选择的UITableViewCells,ios,uitableview,tableviewcell,didselectrowatindexpath,Ios,Uitableview,Tableviewcell,Didselectrowatindexpath,基本上,我现在做的是通过选择单元格并使用heightForRowAtIndexPath更改其高度来扩展单元格,如果我再次选择或选择其他单元格,这些单元格将扩展或恢复正常大小 但是,在单元格展开时,我有一些额外的数据要显示在展开的部分中,请更改单元格的背景颜色,并设置我在tableviewcell子类中定义的一些其他属性。例如,当单元格处于正常高度时,背景为浅绿色。当它膨胀时,它需要是白色的。我设置了tableviewcell子类属性(BOOL),以便在再次循环单元格时(CellForRowati

基本上,我现在做的是通过选择单元格并使用heightForRowAtIndexPath更改其高度来扩展单元格,如果我再次选择或选择其他单元格,这些单元格将扩展或恢复正常大小

但是,在单元格展开时,我有一些额外的数据要显示在展开的部分中,请更改单元格的背景颜色,并设置我在tableviewcell子类中定义的一些其他属性。例如,当单元格处于正常高度时,背景为浅绿色。当它膨胀时,它需要是白色的。我设置了tableviewcell子类属性(BOOL),以便在再次循环单元格时(CellForRowatineXpath),我可以根据需要更新这些属性。不幸的是,我无法找到一种方法来获取在cellForRowAtIndexPath中选择的当前单元格

下面是相关代码。请记住,我希望跟踪当前选定单元格和上一个单元格(如果与当前单元格不同),以便更新这两个单元格的属性。一次只能扩展一个单元格。选择并展开当前单元格后,上一个单元格(如果适用)将收缩回正常高度。我的configureCell方法就是基于BOOL属性来分配属性的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier forIndexPath:indexPath];

    [cell configureCell:self.item[indexPath.row] isCollapsed:cell.isCollapsed];

    return cell;
}


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

    MyCustomCell *currentCell;
    MyCustomCell *previousCell;

    self.currentSelectedIndex = indexPath;
    //assign previous and current if previous is nil
    if(!self.previousSelectedIndex){
        self.previousSelectedIndex = self.currentSelectedIndex;
        currentCell = [tableView cellForRowAtIndexPath:self.currentSelectedIndex];
        currentCell.isCollapsed = NO;
    }
    //we have tapped the same cell as before
    else if(self.previousSelectedIndex == self.currentSelectedIndex){
        previousCell = [tableView cellForRowAtIndexPath:self.previousSelectedIndex];
        previousCell.isCollapsed = YES;
        self.previousSelectedIndex = self.currentSelectedIndex = nil;
    }
    //if they aren't equal, then collapse the previous selected cell
    //and expand the current selected cell
    else{
        previousCell = [tableView cellForRowAtIndexPath:self.previousSelectedIndex];
        previousCell.isCollapsed = YES;

        currentCell = [tableView cellForRowAtIndexPath:self.currentSelectedIndex];
        currentCell.isCollapsed = NO;
        self.previousSelectedIndex = self.currentSelectedIndex;

    }

    [tableView beginUpdates];
  if(self.currentSelectedIndex){
    [tableView reloadRowsAtIndexPaths:@[self.currentSelectedIndex, self.previousSelectedIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}

    [tableView endUpdates];
}
因此,很明显,一旦我们离开此方法,我当前和以前的单元格将被丢弃,因为它们是本地的,但我正在努力解决如何:

a。重新加载单元格,这将导致cellForRowAtIndexPath再次执行(这在尝试使用重新加载的行时有效-但可能我做得不对)

b、 一旦CellForRowatinex开始遍历单元格,如何捕获currentCell和previousCell,以便我可以按照上面所述更新其内容。[dequeueReusableCellWithIdentifier:myIdentifier]只获取一个我显然不想要的新单元格


单元格扩展和收缩良好,因此这不是问题。

是否折叠单元格应由控制器告知视图,而不是在此处询问视图:[cell configureCell:self.item[indexPath.row]isCollapsed:cell.isCollapsed];要么在模型类中有一个属性(如果您觉得它是该对象固有的属性),要么您可以说indexPath是否等于self.currentSelectedIndex[cell configureCell:self.item[indexPath.row]是否合并:否];else[cell-configureCell:self.item[indexPath.row]已合并:YES];是的,我肯定把它弄得更复杂了。我应该只跟踪当前索引,其他任何不是当前索引的内容都将被折叠。我在让cellForRow和heightForRow保持同步时遇到了一些问题,但我正在努力解决。单元格是否应该折叠是控制器应该告诉您视图的事情,而不是您在这里执行时应该询问视图的事情:[cell configureCell:self.item[indexPath.row]isCollapsed:cell.isCollapsed];要么在模型类中有一个属性(如果您觉得它是该对象固有的属性),要么您可以说indexPath是否等于self.currentSelectedIndex[cell configureCell:self.item[indexPath.row]是否合并:否];else[cell-configureCell:self.item[indexPath.row]已合并:YES];是的,我肯定把它弄得更复杂了。我应该只跟踪当前索引,其他任何不是当前索引的内容都将被折叠。我在让cellForRow和heightForRow保持同步方面遇到了一些困难,但我正在努力解决这个问题。