UITableViewSection行指数

我有一个UITableView有三个部分(星期五,星期六,星期日),每个部分都有不同的数据和不同的行数。 选中后,所选行将展开,但具有相同索引的其他两个部分中的行也会展开。 我只想要单个选定的行进行扩展。

有没有办法在我的didSelectRowAtIndexPath方法中获取该部分,并在我的heightForRowAtIndexPath方法中使用它,以便将所有其他行保持在未展开的大小?

以下方法都在我的UITableView子类中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; customCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //Close row if tapped a second time if(self.currentSelection == indexPath.row) { self.currentSelection = -1; } //Otherwise, expand row else { self.currentSelection = indexPath.row; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int rowHeight; if ([indexPath row] == self.currentSelection) { rowHeight = 185; } else rowHeight = 44; return rowHeight; } 

将索引路径保存为您的选择而不仅仅是行。 在您保存的那一刻,比如第2行,每个部分都有一行2,因此该行在所有部分都在扩展。

您可以按如下方式比较索引路径:

 if([indexPath1 isEqual:indexPath2]) // Do your thing 

更好的是, UITableView有一个indexPathForSelectedRow属性,所以你甚至不需要自己的变量来跟踪这个:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int rowHeight; if ([indexPath isEqual:tableView.indexPathForSelectedRow]) { rowHeight = 185; } else rowHeight = 44; return rowHeight; } 

检查NSIndexPath。 该对象包含作为属性的部分。