gpt4 book ai didi

ios - UITableViewCell Frame 在我第一次加载设置后没有改变

转载 作者:行者123 更新时间:2023-11-29 02:10:26 25 4
gpt4 key购买 nike

我使用 xib 创建了一个自定义 UITableViewCell ,并将一些 UILabel 添加到 xib 中。之后,我创建了自己的函数来设置 UILabel 的框架。

在 View Controller 中:

- (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];
}

在 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]];

}

问题是,它不会改变我在第一次加载中指定的标签框架,它会在我滚动后发生变化。我应该如何在第一时间刷新它。

更新了图像

  1. > First Load
  2. > After Scrolling

最佳答案

在你上面的方法最后写上下面的内容

- (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];
}

如果启用了 AutoLayout,则使用

[self updateConstraintsIfNeeded];

首先,如果您使用的是自动版式,则不应触摸框架。使用 AutoLayout 的全部意义在于,框架是根据附加到它们的可满足约束来推断的。

或者在您的 xib 中禁用自动布局,然后您可以以编程方式设置框架。

我建议您应该阅读更多关于如何使用约束的内容,这样您就不需要编写整个代码来设置框架。 AutoLayout 会为您做这件事。

AutoLayout in iOS7

关于ios - UITableViewCell Frame 在我第一次加载设置后没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29338672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com