我今天在我的应用程序遇到了问题。我在View Controller中有一个表。点击Compare
按钮时会出现问题。当我点击比较按钮时,会出现一个小动画,每个单元格都会调整大小以允许显示selected
或not selected
图像。
以下是有问题的视频:https://www.youtube.com/watch?v=6SkIbp1xf0E
问题是,虽然所有cels都应该有默认的not selected
按钮,但有些单元格不会显示图像。
这是我的代码的一部分。
//animate function
-(void)animate{
[[tableView visibleCells] enumerateObjectsUsingBlock:^(CustomCell *cell, NSUInteger idx, BOOL *stop)
{
[cell.myImageView setFrame:CGRectMake(cell.frame.size.width * 0.15 +
cell.myImageView.frame.origin.x, cell.myImageView.frame.origin.y +
cell.frame.size.height * 0.1, cell.myImageView.frame.size.width * 1.25,
cell.myImageView.frame.size.height * 1.25)];
completion:^(BOOL finished)
{
// code to execute when the animation finishes
[UIImageView animateWithDuration:1 animations:^{
//select button animation
cell.compareProduct.image = [UIImage imageNamed:@"deselectedProduct.png"];
cell.compareProduct.hidden = false;
[cell.compareProduct setAlpha:0];
[UIImageView beginAnimations:NULL context:nil];
[UIImageView setAnimationDuration:1.0];
[cell.compareProduct setAlpha:1];
[UIImageView commitAnimations];
}];}
}
这段代码每次都完美无缺地执行。我看到的每个单元格都会显示selected
\ not selected
按钮。滚动时会出现问题。
-(void)tableView:(UITableView *)theTableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (compareActive) //if compare button was pressed
{
[[tableView visibleCells] enumerateObjectsUsingBlock:^(CustomCell *cell, NSUInteger idx, BOOL *stop) {
CGRect someRect = CGRectMake(cell.frame.size.width * 0.15 +
cell.myImageView.frame.origin.x, cell.myImageView.frame.origin.y +
cell.frame.size.height * 0.1, cell.myImageView.frame.size.width * 1.25,
cell.myImageView.frame.size.height * 1.25);
[cell.myImageView setFrame:someRect];
// i was forced to place another moving animation so the user won't notice how the image jumps from the old frame to the new frame
[cell setFrame:CGRectMake(cell.frame.size.width, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
[UIView animateWithDuration:1 animations:^{
[cell setFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
}];}
}
为什么有些细胞没有显示selected
not selected
以及框架为什么会跳跃?另外,不要忘记我正在使用CustomCell。
如果您有更好的解决方案或其他功能,请指出我们。我想要的功能是这样的:我按下compare
按钮,所有表格单元格一次改变它们的帧。
提前致谢。
答案 0 :(得分:0)
我认为[tableView visibleCells]
和willDisplayCell:
功能并不是一个很好的匹配。
在您拨打willDisplayCell:
之后,可能无法立即返回小区点击[tableView visibleCells]
。
我猜你应该在ViewController中使用bool变量,并在通过cellForViewAtIndexPath:
重新加载表视图时查看它。然后当用户在您的比较按钮上显示时,请致电reloadData
。