此问题与以下问题相关:您可以在 UITableViewCell 上设置高度变化的动画吗?
我正在使用以下代码为该问题中解释的 UITableViewCell 的高度变化设置动画:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProductCell *cell = (ProductCell *) [tableView cellForRowAtIndexPath:indexPath];
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[self.selectedIndexes setObject:selectedIndex forKey:indexPath];
if (isSelected) {
cell.addButton.hidden = NO;
}else {
cell.addButton.hidden = YES;
}
// This is where magic happens...
[self.theMenuListTableView beginUpdates];
[self.theMenuListTableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return kCellHeight * 2.0;
}
// Cell isn't selected so return single height
return kCellHeight;
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
NSNumber *selectedIndex = [self.selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
这工作正常,但我想要的是,当单击一个单元格并且高度已设置动画,然后单击另一个单元格时,我希望第一个单元格收缩/折叠到它的原始大小,另一个展开(动画高度变化)。我怎样才能做到这一点?