我的单元格通过使用setExpanded: method调用改变它们的高度来扩展。
然后我调用reloadRowsAtIndexPaths:刷新单元格。
问题是细胞简单地消失并随机重新出现。我怀疑这与索引的工作方式有关。
如果我调用reloadData或开始更新/结束更新,单元格按预期工作,但我失去动画。
- (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:,我制作了一段关于正在发生的事情的快速视频并提取了屏幕截图。
我要做的是展开一个单元格,而不是替换、插入或删除单元格。每个单元格都有一个或多个子视图。单元格的高度取决于它是否“展开”。content视图中添加了子视图,并且它的clipToBounds属性是肯定的。当单元格展开或折叠时,高度值随单元格的帧(包括背景视图和选定的背景视图)而变化。在展开之前、期间和之后,我记录了所有的帧值,它们都与它们的状态和位置一致。






请记住,这通常适用于iOS 4.3,如下所示:

发布于 2012-03-01 12:49:59
@Javy,
谢谢你的问题。我正在开发具有类似行为的表:我的表视图单元格(UITextView)中有以下文本视图(UITableViewCell):
如果需要新行显示文本而不滚动,则展开
所以我发现了同样的问题。在iOS 5.x中,当我开始键入文本时,它突然变得不可见了。但是在iOS 4.x中,一切都很好。
我已经找到了解决办法,而且对我来说效果很好。
解决方案:只是在重新加载特定单元时尝试用UITableViewRowAnimationNone替换动画类型的UITableViewRowAnimationAutomatic。
一些附加代码:在一分钟内重新加载两个单元格:
NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:2];
// collapse previous cell
if( previousCell != nil && [previousCell expandable] )
{
if( [previousCell expanded] ) [previousCell setExpanded:NO];
[indexes addObject:previousIndexPath_];
}
// expand new cell
if( [cell expandable] )
{
[cell setExpanded:YES];
[indexes addObject:indexPath];
}
[tableView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone];希望它能帮到你。
https://stackoverflow.com/questions/9396055
复制相似问题