Ios TableViewCell中的UITextField帧

Ios TableViewCell中的UITextField帧,ios,ipad,Ios,Ipad,为什么不能为tableViewCell文本字段获取正确的帧 myCustomCell有一个leftDetailTF和一个rightDetailTF文本字段 无论我点击哪个单元格/文本字段,下面的log语句都会生成相同的rect坐标。LOG:Frame={470,1},{200,24} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ myCustomCell

为什么不能为tableViewCell文本字段获取正确的帧

myCustomCell有一个leftDetailTF和一个rightDetailTF文本字段

无论我点击哪个单元格/文本字段,下面的log语句都会生成相同的rect坐标。LOG:Frame={470,1},{200,24}

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

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

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

首先得到单元的框架

CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath];
然后获取正确文本字段的框架

myCustomCell *cell = (myCustomCell*)[tableView cellForRowAtIndexPath:indexPath];
CGRect rightDetailTFFrame = cell.rightDetailTF.frame;
然后将两者结合起来:

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

使用数组记录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],........);
}

如果要在superview的superview中检查子视图的框架,可以使用此选项

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

所以,问题是,对于iOS中的UITableView,单元格中所有视图的框架都是固定的。因此,您不能修改这些帧。无论您为单元格设置了什么帧,当自动调用
[cell reloadData]
时,都会恢复该帧

您是否正确初始化了
init
方法中的视图

在您的“myCustomCell”类中,刚好超过了“loadsubviews”方法


在此方法中打印子视图的CGRect,如果帧正确,则在初始化init方法中的视图时一定会出现问题。

call cellForRowAtIndexPath将重新创建单元格或获取缓存单元格,因此无法获取所需内容。您可以在数组中记录textField,要在didselectRowAtIndexPath中调用它,我需要用于rightDetailsTF的rect。rectForRowAtIndexPath返回单元格的rect,而不是单元格中的文本字段。resultFrame origin.x仍然不正确。它看起来像cell.rightDetailTF.origin.x返回一个向左偏移600的点。是否在单元格的LayoutSubview中进行自定义布局?