2012-11-27 27 views
0

爲什麼我無法獲得tableViewCell textField的正確框架?來自TableViewCell的UITextField框架

myCustomCell有一個leftDetailTF和一個rightDetailTF文本字段。

無論我點擊哪些單元格/文本區域,下面的日誌語句都會生成相同的矩形座標。 LOG:幀= {{470,1},{200,24}}

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

myCustomCell *cell = (myCustomCell*)[tableView cellForRowAtIndexPath:indexPath]; 

NSLog(@"Frame = %@",NSStringFromCGRect(cell.rightDetailTF.frame)); 
+0

通話的cellForRowAtIndexPath將rebulid細胞或得到一個緩存單元,所以不要讓你want.you可以在一個陣列記錄文本框,並把它調用didSelectRowAtIndexPath方法。 – zt9788

+0

嘗試rectForRowAtIndexPath: – combinatorial

+0

我需要rect爲rightDetailsTF。 rectForRowAtIndexPath返回單元格的rect而不是單元格中的textField。 – vcirilli

回答

0

如果你想在超視圖的超視圖中檢查子視圖的框架,你可以使用它。

CGRect rect = [cell.rightDetailTF.superview convertRect:cell.rightDetailTF.frame toView:self.view]; 
NSLog(@"Rect:%@",NSStringFromCGRect(rect)); 
+0

這張票 - 非常感謝您的幫助。 – vcirilli

+0

哦。不用謝。 – SARANGA

0

首先獲得小區的幀...

CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath]; 

然後得到正確的文本字段的幀

CGRect resultFrame = CGRectMake(cellFrame.origin.x + rightDetailTFFrame.origin.x, cellFrame.origin.y + rightDetailTFFrame.origin.y, rightDetailTFFrame.size.width, rightDetailTFFrame.size.height); 
+0

resultFrame origin.x仍然不正確。它看起來像cell.rightDetailTF.origin.x返回了偏移通過像600 – vcirilli

+0

你在你的細胞中layoutSubviews做自定義佈局留下了點? – combinatorial

0

使用數組到:...

myCustomCell *cell = (myCustomCell*)[tableView cellForRowAtIndexPath:indexPath]; 
CGRect rightDetailTFFrame = cell.rightDetailTF.frame; 

兩個再結合記錄rect。

-(UITableViewCell*)cell.......:IndexPath... 
{ 
     CGRect rect = cell.rightDetailTF.cellFrame; 
     NSMutableDictionary* dic = [NSMutableDictionary alloc] init...:4]; 
     [dic setObject:[NSNumber numberWithFloat:rect.origin.x] forKey:...1]; 
     //... rect.origin.y,rect.size.with,rect.size.height 
     [arr addObject:dic]; 
     [dic release]; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
     NSMutableDictionary* dic = [arr objectAtIndex:indexPath.row]; 
     CGRect rect = CGRectMake([dic objectForKey:...1],........); 
} 
0

那麼,事情是,在iOS的一個UITableView你的框架是固定的小區中的所有意見。因此,你CAN NOT修改這些框架。無論您爲單元格設置了哪個框架,它都會在自動調用[cell reloadData]時恢復。

您是否正確初始化了init方法中的視圖?

剛過,乘坐「loadsubviews」的方法在你的「myCustomCell」類。

打印出的CGRect在這種方法中,子視圖,如果幀中,正確的,你必須在你初始化在init方法的看法有問題。