2011-02-24 26 views
3

这是一个不同的问题,但我认为是错误的没有错,所以我调整了标题以反映我真正想到的。访问现有的UITableViewCell

我正在处理一个动画,它需要在正常的UITableViewCell的cell.imageView中将图像的副本移动到屏幕底部的选项卡栏上,以模拟将物品放入购物车。我可以复制图像并将副本作为窗口的子视图,但我无法弄清楚如何获得原始图像的绝对位置,以便将它放在顶部作为动画的起点。

图像始终显示在屏幕的顶部,好像置于0,0一样。

对不起,这个问题没有问题,我相信我错过了一些非常明显的东西。

- (空)的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 

}

+0

我想我想通了。当我调用[自我tableView:tableView cellForRotAtIndexPath ....]。我没有获得对现有单元格的参考。我从头开始获得一个新的“复制”单元。所以当我试图获得关于细胞位置的信息时,我实际上已经获得了我的细胞拷贝的位置,这个位置还没有正确铺设。 – Donk 2011-02-25 01:31:14

回答

5

所以我这样做都是错的。 我正在使用相同的内容而不是使用当前单元格的新单元格。我通过在单元格创建时向其添加标签来解决此问题。

cell.tag = [indexPath row];

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

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

然后我刚更换此线

的UITableViewCell *细胞= [自的tableView:的tableView的cellForRowAtIndexPath:indexPath];

随着

的UITableViewCell *细胞=(的UITableViewCell *)[self.tableView viewWithTag:(1000 + [indexPath行])];

现在我得到了已经在表格视图中的实际单元格的引用,并且我得到了正确的帧值。

2

超过1段,这可能提供更独特的标签

([indexPath部] +1)避免通过部分0 (1000 *([indexPath部] +1))给出的1000步乘以2000等。

在cellForRowAtIndexPath设置标签... cell.tag =((1000 *([indexPath section] +1))+ [indexPath row]);

在didSelectRowAtIndexPath中检索它...(UITableViewCell *) [playlistTableView viewWithTag:((1000 *([indexPath section] +1))+ [indexPath row])];

假设您的列表不超过1000个单元格中的每个部分。

这是一个很好的问题,Donk提供了一个非常有用的答案,非常感谢发布的详细信息。

+0

我喜欢Donk使用视图标签的想法。这只适用于可见细胞,当然,因为它们被回收。但是,在响应UI事件时,这很好! 快速搜索[将两个整数映射到唯一值](http://stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way)出现_Szudzik的function_:'a> = b? a * a + a + b:a + b * b;'其中a,b> = 0.在您的部分和行中弹出以获取单元格的唯一视图ID。 – Nuthatch 2014-01-04 03:10:54