0

为什么我无法为 tableViewCell 文本字段获取正确的框架?

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

无论我点击什么单元格/文本字段,下面的日志语句都会生成相同的矩形坐标。日志:帧 = {{470, 1}, {200, 24}}

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

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

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

4 回答 4

0

首先获取单元格的框架...

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);
于 2012-11-27T04:23:43.273 回答
0

使用数组记录矩形。

-(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],........);
}
于 2012-11-27T06:54:38.663 回答
0

如果您想在超级视图的超级视图中检查子视图的框架,您可以使用它。

  CGRect rect = [cell.rightDetailTF.superview convertRect:cell.rightDetailTF.frame toView:self.view];
NSLog(@"Rect:%@",NSStringFromCGRect(rect));
于 2012-11-27T06:58:17.160 回答
0

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

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

只需覆盖“myCustomCell”类中的“loadsubviews”方法即可。

在这个方法中打印出子视图的CGRect,如果框架不正确,那么在init方法中初始化视图时一定有问题。

于 2012-11-27T07:12:27.417 回答