【发布时间】:2015-01-21 05:44:31
【问题描述】:
我正在尝试同时为自定义 UITableViewCell 和包含它的 UITableView 设置动画。
单击时,UITableViewCell 的子视图会稍微重新排列,以使单元格的高度增加。我希望单元格内的重新排列与调整该单元格的 UITableView 插槽的大小同时发生,这样它就不会与其下方的单元格重叠。
再次点击时,会发生相反的情况。
这是我迄今为止尝试过的。
UITableView
点击 UITableView 时,我使用didSelectRowAtIndexPath 更新selectedPath 属性:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
self.selectedPath = nil;
} else {
self.selectedPath = indexPath;
}
[tableView reloadData];
}
注意,它调用reloadData,它会触发每个可见单元重新获取原型并根据是否被选中进行配置:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QueueItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QueueCell" forIndexPath:indexPath];
[cell setImage:[UIImage imageNamed:[self.imageList objectAtIndex:indexPath.row]]];
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
[cell makeSelected:YES];
} else {
[cell makeSelected:NO];
}
return cell;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath {
CGFloat height = 210;
if (indexPath.row == [self.imageList count] - 1) {
height += 10;
}
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
height += 35;
}
return height;
}
UITableViewCell 子类
在自定义单元格类中,它的动画在它被选中或未被选中时发生:
- (void)makeSelected:(BOOL)selected {
if (selected) {
[UIView animateWithDuration:0.5 animations:^{
self.customImage.frame = CGRectMake(5, 5, 310, 210);
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.customImage.frame = CGRectMake(10, 10, 300, 200);
}];
}
}
发生了什么
表格视图立即捕捉到为单元格分配的新高度,然后单元格的内容会慢慢动画到新状态。我希望这两件事同时发生,并且顺利进行。
【问题讨论】:
-
点击单元格时需要重新加载所有单元格或特定单元格动画
-
我正在重新加载所有单元格。见
[tableView reloadData]。
标签: ios objective-c iphone uitableview