复制UITableViewCell - iPhone

时间:2010-03-17 13:34:46

标签: iphone objective-c uitableview duplicates

我想为UITableView的单元格创建一个效果。效果是:复制单元格并移动复制的单元格(原始单元格保留在其位置)。我的问题是复制单元格......

我试过了:

代码:

UITableViewCell *animatedCell = [[UITableViewCell alloc] init];
animatedCell = [[self cellForRowAtIndexPath:indexPath] copy];

但UIView似乎没有实现副本......我怎么能这样做?

由于

2 个答案:

答案 0 :(得分:3)

如果您只需要将单元格的重复图像用于动画(不是包含所有子视图的真实视图),则只需复制单元格图像:

UITableViewCell * aCell = [tableView cellForRowAtIndexPath:indexPath];
UIGraphicsBeginImageContext(aCell.frame.size);
[aCell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *aCellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * imageView = [[UIImageView alloc] initWithImage:aCellImage];

或者,如果您需要真实视图创建它。并且考虑到UITableView不会创建单元格,您可以在tableView dataSource的 tableView:cellForRowAtIndexPath:方法中执行此操作。因此,使用相同的代码创建具有指定indexPath的另一个单元格...但是不要创建UITableViewCell对象,而是创建UIView并将其添加到单元格的containsView。当你需要复制单元格时,创建相同UIView的另一个实例并在动画中使用它。

答案 1 :(得分:0)

您通常不希望手动分配单元格(tableView:tableView cellForRowAtIndexPath:delegate方法除外)。相反,在数据模型中进行所需的任何更改以反映新单元格,然后告诉tableview插入新行:

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];