Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何以编程方式将子视图定位和调整大小到单元格';s内容视图_Objective C_Uitableview_Layout - Fatal编程技术网

Objective c 如何以编程方式将子视图定位和调整大小到单元格';s内容视图

Objective c 如何以编程方式将子视图定位和调整大小到单元格';s内容视图,objective-c,uitableview,layout,Objective C,Uitableview,Layout,我想在tableView:cellForRowAtIndexPath:处获取单元格框架,以便定位和调整要添加到UITableViewCell的内容视图的视图的大小。但是,self.frame总是(0,0320,44) 我知道您可以在layoutSubviews中获得正确的frame(多亏了),但是如果我在那里添加子视图,那么每次重复使用单元格时都会这样做,而不是像上的“官方”示例中那样只重复一次 在该示例中,它们使用硬编码帧添加了一些视图,如: mainLabel = [[UILabel all

我想在
tableView:cellForRowAtIndexPath:
处获取单元格
框架
,以便定位和调整要添加到
UITableViewCell
内容视图
的视图的大小。但是,self.frame总是
(0,0320,44)

我知道您可以在
layoutSubviews
中获得正确的
frame
(多亏了),但是如果我在那里添加子视图,那么每次重复使用单元格时都会这样做,而不是像上的“官方”示例中那样只重复一次

在该示例中,它们使用硬编码帧添加了一些视图,如:

mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
我猜这个例子已经过时了,因为它们应该使用约束,或者至少使用实际的单元格
计算子视图的大小(这在当时可能是不可能的)


注意:这让我想起了Android中使用的设计模式。

这应该返回边框

CGRect cellFrame = cell.bounds;
然后你可以像这样使用它

cellFrame.size.width;
cellFrame.size.height;
cellFrame.origin.x;
cellFrame.origin.y;
等等。。。尽管origin.x和origin.y应分别为0

也许您应该在UITableViewCell子类中计算单元格高度并使用

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
在您的学员中找到正确的高度。

我使用两种解决方案:

1) 当我显式地设置帧时 我假装框架:

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

// Use whatever height you like for your cell
// Should be the same value you return in tableView:heightForRowAtIndexPath:
CGFloat cellHeight = XXX;

CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, cellHeight);
2)当我使用自动布局时 我只是将我的视图和约束添加到
self.contentView

[someView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:someView];
[self.contentView addConstraints:someConstraints];

但请检查以防万一。

它不起作用,因为默认情况下,rect是(0,0320,44)。在layoutSubviews中,您得到了正确的边界。但是apple docs中的示例代码是无用的,因为您无法在init方法中预构建视图。