我有一个 UITableView,其中每个单元格在其 contentView 中包含一个 UIView,我们称它为 V。V 也有子视图,一个 UIImageView 和 UILabel.UIImage 只是一个白色圆角矩形我希望我的单元格(连同 UIImageView)在何时展开和缩小选择。我在 didSelectRowAtIndexPath 和 heightForRowAtIndexPath 方法中添加了一些代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber * key = [NSNumber numberWithInt:indexPath.row];
UITableViewCell *cell = [cells objectAtIndex:indexPath.row];
UIView * v = [[[cell contentView] subviews] objectAtIndex:0];
UIImageView * image = [[v subviews] objectAtIndex:0];
_tappedIndex = [key intValue];
_expanded = [_tappedCells objectForKey:key] == nil;
NSLog(@"view's old params: %@", [GGStackPanel printFrameParams:v]); //prints out the frame params
NSLog(@"image's old params: %@", [GGStackPanel printFrameParams:image]);
if (_expanded)
{
[_tappedCells setObject:key forKey:key];
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width, v.frame.size.height*1.5);
image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y, image.frame.size.width, v.frame.size.height + 73);
}
else
{
[_tappedCells removeObjectForKey:key];
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width, v.frame.size.height/1.5 );
image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y, image.frame.size.width, v.frame.size.height - 113);
}
NSLog(@"view's new params: %@", [GGStackPanel printFrameParams:v]);
NSLog(@"image's new params: %@", [GGStackPanel printFrameParams:image]);
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *v = [_cells objectAtIndex:indexPath.row];
CGFloat height = v.frame.size.height;
if (indexPath.row == _tappedIndex)
{
if (_expanded)
{
height = v.frame.size.height * 1.5;
}
else
{
height = v.frame.size.height/1.5;
}
}
return height;
}
单元格框架和 V 正在扩展,我记录了它们的框架参数。但是 imageView 始终保持不变,即使您在每次选择单元格时更改其框架。它的框架在 didSelectRowAtIndexPath 方法中发生了变化,但是当你再次点击同一个单元格时,它说它的框架与上次相比没有改变。
如果我放置另一个 UIView 而不是 imageView,它会随其父 View 和单元格展开和缩小。因此,我想出了一个非常奇怪的解决方案:剪下圆角矩形,将狭窄的顶部和底部部分保留为 imageView。然后在中间放一个空白的UIView,颜色和矩形一样。现在我的“矩形”正在扩展,因为空白 UIView 扩展并且这两个 imageViews 上下移动。我不喜欢这个解决方案,因为如果您将手机切换到横向模式,或者尝试从横向模式扩展单元格并返回纵向模式,UI 就会变得混乱。
有什么意见、建议吗?