gpt4 book ai didi

iphone - 当按下后退按钮返回到 rootViewController 时,UILabel sizeToFit 未保留

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:38 24 4
gpt4 key购买 nike

所以我的 rootViewController 包含一个带有自定义 UICollectionViewCell 的 UICollectionView。我在单元格上有一个 header 的标签,该标签永远不会更改,并且我有一个多行标签构成了单元格其余部分的主体。当我启动应用程序时,它会正确调用 sizeToFit 并顶部对齐所有内容,因此它不会居中。我单击其中一个单元格并转到下一个 View ,然后如果我单击后退按钮返回到 rootViewController,它会运行 viewWillAppear 方法并重新加载数据,但 sizeToFit 不起作用,并且多行上的所有内容标签居中对齐。它仍然是多行,但如果只有几行,它位于标签的中心,看起来不太好。我怎样才能保持这个,所以它是一致的。我有一个左侧菜单,它将重新加载 rootViewController,它将再次正确定位标签,但是一旦我从 secondViewController 按下后退按钮,它就不再对齐。有没有办法让它清除所有单元格并重新加载它们。我试过 [collectionView reloadData];在 viewWillAppear 中,它不起作用,当前在 connectionDidFinish 的末尾调用它,网络连接是从 viewWillAppear 方法调用的。感谢您提供任何帮助。

这是自定义的 UICollectionViewCell 代码

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

}
return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[contentLabel setNumberOfLines:0];
[contentLabel sizeToFit];
}

Root View Controller

-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"LocationListCell";
LocationList *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier
forIndexPath:indexPath];

[cell.titleBar setText:[value objectAtIndex:indexPath.row]];
NSArray *tempObject = [[NSArray alloc] initWithArray:[self getData:[key
objectAtIndex:indexPath.row]]];

NSMutableString *tempString = [[NSMutableString alloc] init];
for (int i = 0; i < [tempObject count]; i++)
{
[tempString appendString:[tempObject objectAtIndex:i]];
[tempString appendString:@"\r"];
}
//[cell.contentLabel sizeToFit];
//[cell.contentLabel setNumberOfLines:0];
[cell.contentLabel setText:tempString];
return cell;
}

最佳答案

将此动态 UILable 与此自定义方法一起使用...

只需在您的.m 文件中添加下面的方法

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}

并像下面那样使用它..

   UILabel *yourLable = [[UILabel alloc]init];
[yourLable setFrame:CGRectMake(110, 31, 200, 50)];
[yourLable setText:tempString];
yourLable.lineBreakMode = UILineBreakModeWordWrap;
yourLable.numberOfLines = 0;
yourLable.font = [UIFont fontWithName:@"Helvetica" size:12];

yourLable.frame = CGRectMake(yourLable.frame.origin.x, yourLable.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:yourLable.text :yourLable.font :200 :UILineBreakModeWordWrap] );


yourLable.textColor = [UIColor darkGrayColor];

cell.contentLabel = yourLable;

希望这对你有帮助...

关于iphone - 当按下后退按钮返回到 rootViewController 时,UILabel sizeToFit 未保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964918/

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