我在iPhone应用程序中使用的是UITableView
,而且有一个属于一组的人的列表。 我但愿这样,以便当用户单击特定的人(从而选择单元格)时,单元格的高度会增长,以显示多个用于编辑该人的属性的UI控件。 动画
这可能吗? atom
我只是经过一点技巧解决了这个问题: spa
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值为止:-) code
添加属性以跟踪所选单元格 orm
@property (nonatomic) int currentSelection;
将其设置为(例如) viewDidLoad
的哨兵值,以确保UITableView
从“正常”位置开始 索引
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //sentinel self.currentSelection = -1; }
在heightForRowAtIndexPath
,能够设置选定单元格所需的高度 ip
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ int rowHeight; if ([indexPath row] == self.currentSelection) { rowHeight = self.newCellHeight; } else rowHeight = 57.0f; return rowHeight; }
在didSelectRowAtIndexPath
,保存当前选择并保存动态高度(若是须要) get
- (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
将选择索引设置回前哨值,并将单元设置为动画形式 animation
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { // do things with your cell here // sentinel self.currentSelection = -1; // animate [tableView beginUpdates]; [tableView endUpdates]; } }
我发现了一个很是简单的解决方案,这是我正在研究的UITableView
的反作用。 博客
将像元高度存储在一般经过tableView: heightForRowAtIndexPath:
来报告原始高度的变量中,而后在要对高度变化进行动画处理时,只需更改变量的值并调用它便可。
[tableView beginUpdates]; [tableView endUpdates];
您会发现它不会彻底重载,但足以让UITableView
知道它必须重绘单元格,获取单元格的新高度值....猜猜是什么? 为您动画化变化。 甜。
我在博客上有更详细的解释和完整的代码示例。Animate UITableView单元格高度变化
我不知道有关连续调用beginUpdates / endUpdates的全部内容是什么,您能够只使用-[UITableView reloadRowsAtIndexPaths:withAnimation:]
。 这是一个示例项目 。
尝试这样作是为了扩展按索引行
@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; }