У меня есть ячейки, которые расширяются путем изменения их высоты с помощью вызова метода setExpanded:
Затем я вызываю reloadRowsAtIndexPaths: для обновления ячеек.
Проблема заключается в том, что ячейки просто исчезают и случайным образомвновь появляются.Я подозреваю, что это связано с тем, как работает индексация.
Если я вызываю reloadData или beginUpdates / endUpdates, ячейки работают как положено, но я теряю анимацию.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
JVCell *cell = (JVCell*)[tableView cellForRowAtIndexPath:indexPath];
JVCell *previousCell = nil;
if( previousIndexPath_ != nil ) // set to nil in viewDidLoad
{
previousCell = (JVCell*)[tableView cellForRowAtIndexPath:previousIndexPath_];
}
// expand or collapse cell if it's the same one as last time
if( previousCell != nil && [previousIndexPath_ compare:indexPath] == NSOrderedSame && [previousCell expandable] )
{
[previousCell setExpanded:![cell expanded]];
NSArray *indexPathArray = [NSArray arrayWithObject:previousIndexPath_];
[tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
}
else
{
// collapse previous cell
if( previousCell != nil && [previousCell expandable] )
{
if( [previousCell expanded] ) [previousCell setExpanded:NO];
NSArray *indexPathArray = [NSArray arrayWithObject:previousIndexPath_];
[tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
}
// expand new cell
if( [cell expandable] )
{
[cell setExpanded:YES];
NSArray *indexPathArray = [NSArray arrayWithObject:indexPath];
[tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
previousIndexPath_ = indexPath;
// works as expected, but I lose the animations
//[tableView reloadData];
// works as expected, but I lose the animations
//[tableView beginUpdates];
//[tableView endUpdates];
}
РЕДАКТИРОВАТЬ: обновлено, чтобы включить методы cellForRowAtIndexPath и heightForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
JVCellSectionData *sectionData = [sections_ objectAtIndex:section]; // NSArray of SectionData objects
NSArray *cellArray = [sectionData cells]; // NSArray of cells
UITableViewCell *cell = [cellArray objectAtIndex:row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
JVCellSectionData *sectionData = [sections_ objectAtIndex:section];
NSArray *cellArray = [sectionData cells];
JVCell *cell = [cellArray objectAtIndex:row];
return [cell cellHeight]; // changed when selected with setExpanded: method
}
Редактировать 2: Я сделал Quicktime-видео о происходящем и извлек снимки экрана.
Я пытаюсь расширить ячейку, а не заменять, вставлять или удалять ячейки.Каждая ячейка имеет одно или несколько подпредставлений.Высота ячейки изменяется в зависимости от того, «расширена» она или нет.К представлению содержимого добавлены подпредставления, и его свойство clipToBounds имеет значение YES.Когда ячейки расширяются или сворачиваются, значение высоты изменяется вместе с рамкой ячейки (включая вид фона и выбранный вид фона).Я зарегистрировал все значения кадров до, во время и после расширения, и все они соответствуют их состоянию и положению.
![sectioned table view design](https://i.stack.imgur.com/LFy2P.png)
![cell tapped](https://i.stack.imgur.com/jhjH4.png)
![cell expanding](https://i.stack.imgur.com/n6HSf.png)
![cell disappeared](https://i.stack.imgur.com/xy5oJ.png)
![cell retracts](https://i.stack.imgur.com/3EpRh.png)
![cell visible again](https://i.stack.imgur.com/x9fDR.png)
Имейте в виду, что это нормально работает на iOS 4.3, как показано ниже:
![comaprison ios 4 to ios 5](https://i.stack.imgur.com/TmeJI.png)