2011-02-24 49 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