我在iPhone应用程序中使用的是UITableView
,并且有一个属于一组的人的列表。 我希望这样,以便当用户单击特定的人(从而选择单元格)时,单元格的高度会增加,以显示多个用于编辑该人的属性的UI控件。
这可能吗?
#1楼
我只是通过一点技巧解决了这个问题:
static int s_CellHeight = 30;
static int s_CellHeightEditing = 60;
- (void)onTimer {
cellHeight++;
[tableView reloadData];
if (cellHeight < s_CellHeightEditing)
heightAnimationTimer = [[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(onTimer) userInfo:nil repeats:NO] retain];
}
- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isInEdit) {
return cellHeight;
}
cellHeight = s_CellHeight;
return s_CellHeight;
}
当我需要扩大像元高度时,我设置isInEdit = YES
并调用方法[self onTimer]
,它将对像元增长进行动画处理,直到达到s_CellHeightEditing值为止:-)
#2楼
添加属性以跟踪所选单元格
@property (nonatomic) int currentSelection;
将其设置为(例如) viewDidLoad
的哨兵值,以确保UITableView
从“正常”位置开始
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//sentinel
self.currentSelection = -1;
}
在heightForRowAtIndexPath
,可以设置选定单元格所需的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int rowHeight;
if ([indexPath row] == self.currentSelection) {
rowHeight = self.newCellHeight;
} else rowHeight = 57.0f;
return rowHeight;
}
在didSelectRowAtIndexPath
,保存当前选择并保存动态高度(如果需要)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// set selection
self.currentSelection = indexPath.row;
// save height for full text label
self.newCellHeight = cell.titleLbl.frame.size.height + cell.descriptionLbl.frame.size.height + 10;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
}
在didDeselectRowAtIndexPath
将选择索引设置回前哨值,并将单元设置为动画形式
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// sentinel
self.currentSelection = -1;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
}
#3楼
我发现了一个非常简单的解决方案,这是我正在研究的UITableView
的副作用。
将像元高度存储在通常通过tableView: heightForRowAtIndexPath:
来报告原始高度的变量中,然后在要对高度变化进行动画处理时,只需更改变量的值并调用它即可。
[tableView beginUpdates];
[tableView endUpdates];
您会发现它不会完全重载,但足以让UITableView
知道它必须重绘单元格,获取单元格的新高度值....猜猜是什么? 为您动画化变化。 甜。
我在博客上有更详细的解释和完整的代码示例。Animate UITableView单元格高度变化
#4楼
我不知道有关连续调用beginUpdates / endUpdates的所有内容是什么,您可以只使用-[UITableView reloadRowsAtIndexPaths:withAnimation:]
。 这是一个示例项目 。
#5楼
尝试这样做是为了扩展按索引行
@property (nonatomic) NSIndexPath *expandIndexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if ([indexPath isEqual:self.expandedIndexPath])
return 100;
return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *modifiedRows = [NSMutableArray array];
if ([indexPath isEqual:self.expandIndexPath]) {
[modifiedRows addObject:self.expandIndexPath];
self.expandIndexPath = nil;
} else {
if (self.expandedIndexPath)
[modifiedRows addObject:self.expandIndexPath];
self.expandIndexPath = indexPath;
[modifiedRows addObject:indexPath];
}
// This will animate updating the row sizes
[tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
// Preserve the deselection animation (if desired)
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewControllerCellReuseIdentifier];
cell.textLabel.text = [NSString stringWithFormat:@"I'm cell %ld:%ld", (long)indexPath.section, (long)indexPath.row];
return cell;
}
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3164588