我正在尝试将动画放在 UITableViewCell 中。动画是onClick table view cell 把tableCell 的frame 变成tableview frame。
我有以下代码:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell*cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.frame.size.height == tableView.bounds.size.height){
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}else{
cell.frame = tableView.bounds;
[[cell superview] bringSubviewToFront:cell];
}
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
它工作正常,但在更改单元格的框架后,我也可以点击我不想要的另一个单元格。我怎样才能做到这一点?
最佳答案
一种可能的解决方案是声明一个变量,该变量将保存像这样的扩展单元格的 indexPath 数组
// Will hold the indexPath of expanded cell
var expandedCell : [IndexPath] = []
第二件事是添加和删除展开/未展开的单元格,为此您必须更新您的 UITableView 委托(delegate) didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let index = expandedCell.firstIndex { (localIndexPath) -> Bool in
return indexPath == localIndexPath
}
if let index = index {
expandedCell.remove(at: index)
} else {
expandedCell.append(indexPath)
}
tableviewMessageList.reloadRows(at: [indexPath], with: .fade)
}
最后,您必须添加另一个 UITableView 委托(delegate) heightForRowAt
以返回单元格的高度,如果单元格的 indexPath 在数组中,它将返回扩展后的大小,否则返回单元格的正常大小像这样:-
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if expandedCell.contains(indexPath) {
return tableView.frame.height
}
return 200.0 //NormalHeight
}
Note: My answer is in Swift but the same principle will apply for Objective-C you just need to change the Syntax.
关于ios - UITableViewCell 上的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214770/