UITableViewCell 框架在我第一次加载后不会改变 [英] UITableViewCell Frame Doesn't change after i set in first load
问题描述
我用 xib 创建了一个自定义的 UITableViewCell,并在 xib 中添加了一些 UILabel.之后,我创建了自己的函数来设置 UILabel 的框架.
I have created a custom UITableViewCell with xib and add in few UILabel into xib. After that i created my own function to set the frame of the UILabel.
在视图控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath];
[cell setLabelFrame:self.labelFloor.frame
unit:self.labelUnit.frame
type:self.labelType.frame
sqft:self.labelsqft.frame
price:self.labelPrice.frame
facing:self.labelFacing.frame
view:self.labelView.frame
status:self.labelStatus.frame
labelHeight:labelHeight];
}
IN ListTableViewCell:
- (void)setLabelFrame:(CGRect)floor
unit:(CGRect)unit
type:(CGRect)type
sqft:(CGRect)sqft
price:(CGRect)price
facing:(CGRect)facing
view:(CGRect)view
status:(CGRect)status
labelHeight:(CGFloat)labelHeight {
float pointX = 0;
CGFloat fontSize = 13;
// Create Floor label
pointX = floor.origin.x;
[self.lb_floor setFrame:CGRectMake(pointX, 0, floor.size.width, labelHeight)];
[self.lb_floor setTextColor:[UIColor blackColor]];
self.lb_floor.textAlignment = NSTextAlignmentCenter;
[self.lb_floor setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create Unit label
pointX = unit.origin.x;
[self.lb_unit setFrame:CGRectMake(pointX, 0, unit.size.width, labelHeight)];
[self.lb_unit setTextColor:[UIColor blackColor]];
self.lb_unit.textAlignment = NSTextAlignmentCenter;
[self.lb_unit setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create Type Label
pointX = type.origin.x;
[self.lb_type setFrame:CGRectMake(pointX, 0, type.size.width, labelHeight)];
[self.lb_type setTextColor:[UIColor blackColor]];
self.lb_type.textAlignment = NSTextAlignmentCenter;
[self.lb_type setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create Sq.Ft. Label
pointX = sqft.origin.x;
[self.lb_sqft setFrame:CGRectMake(pointX, 0, sqft.size.width, labelHeight)];
[self.lb_sqft setTextColor:[UIColor blackColor]];
self.lb_sqft.textAlignment = NSTextAlignmentCenter;
[self.lb_sqft setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create Price Label
pointX = price.origin.x;
[self.lb_price setFrame:CGRectMake(pointX, 0, price.size.width, labelHeight)];
[self.lb_price setTextColor:[UIColor blackColor]];
self.lb_price.textAlignment = NSTextAlignmentCenter;
[self.lb_price setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create Facing Label
pointX = facing.origin.x;
[self.lb_facing setFrame:CGRectMake(pointX, 0, facing.size.width, labelHeight)];
[self.lb_facing setTextColor:[UIColor blackColor]];
self.lb_facing.textAlignment = NSTextAlignmentCenter;
[self.lb_facing setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create View Label
pointX = view.origin.x;
[self.lb_view setFrame:CGRectMake(pointX, 0, view.size.width, labelHeight)];
[self.lb_view setTextColor:[UIColor blackColor]];
self.lb_view.textAlignment = NSTextAlignmentCenter;
[self.lb_view setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
// Create Status Label
pointX = status.origin.x;
[self.lb_status setFrame:CGRectMake(pointX, 0, status.size.width, labelHeight)];
[self.lb_status setTextColor:[UIColor blackColor]];
self.lb_status.textAlignment = NSTextAlignmentCenter;
[self.lb_status setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];
}
问题是,它不会像我在第一次加载时那样改变标签框架,而是在我滚动后改变.我应该如何在第一时间刷新它.
The problem is, it doesn't change the label frame as i specific in the first load, it changes after i scrolling. How should i refresh it in first laod.
更新图片
推荐答案
在你上面的方法里写在最后
In your method above write the following in the end
- (void)setLabelFrame:(CGRect)floor
unit:(CGRect)unit
type:(CGRect)type
sqft:(CGRect)sqft
price:(CGRect)price
facing:(CGRect)facing
view:(CGRect)view
status:(CGRect)status
labelHeight:(CGFloat)labelHeight {
//setting of frames...
.
.
.
[self layoutIfNeeded];
}
如果启用了自动布局,则使用
if AutoLayout is enabled, then use
[self updateConstraintsIfNeeded];
首先,如果您使用自动布局,您不应该接触框架.使用 AutoLayout 的全部意义在于,框架是根据附加到它们的可满足约束来推断的.
Well on the first hand you shouldn't be touching frames if you are using autolayout. The whole point of using AutoLayout is that frames are inferred based on the satisfiable constraints attached to them.
或者在您的 xib 中禁用自动布局,然后您可以以编程方式设置框架.
Or else disable autolayout in your xib and then you can set frames programmatically.
我建议你应该阅读更多关于如何使用约束的内容,然后你就不需要编写整个代码来设置框架了.AutoLayout 会为您做到这一点.
I suggest you should read more on how to use constraints and then you wouldn't be needing to write this whole code for setting frames. AutoLayout would just do that for you.
这篇关于UITableViewCell 框架在我第一次加载后不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!