ios - 访问现有的 UITableViewCell

标签 ios animation uiview uitableview coordinates

这是一个不同的问题,但我认为错的并没有错,所以我调整了标题以反射(reflect)我真正想出的内容。

我正在制作一个动画,该动画在普通 UITableViewCell 的 cell.imageView 中获取图像副本并将其移动到屏幕底部的选项卡栏,以模拟将商品放入购物车。我可以复制图像并将副本作为窗口的 subview ,但我不知道如何获取原始图像的绝对位置,因此我可以将其放在顶部作为动画的起点。

图像始终显示在屏幕顶部,就好像放置在 0,0 处一样。

对于这个问题的笨拙性质,我深表歉意,我确定我遗漏了一些非常明显的东西。

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
UIImageView *originalImgView = cell.imageView;
UIImageView *img = [[[UIImageView alloc] initWithImage: cell.imageView.image] autorelease];

NSLog(@"Original %@", originalImgView.bounds);  // Shows "Original (null)" at runtime.
NSLog(@"Window Points %@", [self.view.window convertPoint:originalImgView.frame.origin toView:nil]);
NSLog(@"Cell Points: %@", cell.bounds);  // Shows "Original (null)" at runtime.

[img setFrame:CGRectMake( originalImgView.bounds.origin.x, originalImgView.bounds.origin.y ,
        cell.imageView.image.size.width, cell.imageView.image.size.height)];

[self.view.window addSubview:img];

NSLog(@"Selected Cell: %@", cell); // Shows cell info at run time, works

}

最佳答案

所以我做错了。 我正在制作一个具有相同内容的新单元格,而不是使用当前单元格。我通过在创建单元格时向单元格添加标签来修复此问题。

cell.tag = [indexPath row];

除了当 [indexPath row] == 0 时给了我一个问题。所以我给它加了 1000,那个问题就消失了。

cell.tag = (1000 +[indexPath row]);

然后我就替换了这一行

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:(1000 + [indexPath row])];

现在我得到了对 TableView 中已有的实际单元格的引用,并且我得到了正确的帧值。

关于ios - 访问现有的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5111583/

相关文章:

ios - 操作错误 :@selector in app

ios - 在 iOS 应用程序运行时获取 NSInternalInconsistencyException

css - 边界半径在过渡期间不起作用

animation - Three.js 克隆 FBX 动画

iOS 以编程方式创建 View

ios - UI 元素未显示它们在 xcode 中 Storyboard 上的实际位置

iphone - iOS 7 MKOverlayRenderer 不工作

swift - 如何根据要求在 SceneKit 中添加动画

ios - 如何将渐变应用于多个自定义 View ?

objective-c - iOS - viewForFooterInSection 粘在 UITableView 的底部