当点击返回按钮回到根视图控制器时,UILabel的sizeToFit属性不会保留。

3
我的rootViewController包含一个带有自定义UICollectionViewCell的集合视图。我在单元格上有一个标签用作标题,它不应该改变,并且我有一个多行标签组成了其他单元格的正文。当我启动应用时,它会正确调用sizeToFit并使所有内容顶部对齐,因此它不是居中的。我点击其中一个单元格并进入下一个视图,然后如果我点击返回按钮回到rootViewController,则会运行viewWillAppear方法并重新加载数据,但sizeToFit不起作用,多行标签上的所有内容都变成居中对齐。它仍然是多行的,但如果只有几行,它会坐落在标签的中心位置,看起来不好。如何保持一致?我有一个左侧菜单,将重新加载rootViewController,这将再次正确定位标签,但是一旦我从secondViewController点击返回按钮后,它就不再对齐。是否有一种清除所有单元格并重新加载它们的方法?我已经尝试在viewWillAppear中使用[collectionView reloadData],但它不起作用,目前在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];
}

根视图控制器

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

分享一些与在单元格上创建标签相关的代码... - Vishal
请在您的问题中更新代码。 - rmaddy
@Jarod 你想要什么?是想要动态的UILable吗? - Paras Joshi
1
那我该怎么做呢?我的意思是,当它第一次加载时,它看起来很正常,为什么当第二个视图控制器从堆栈中弹出时,rootViewController不会保持其之前推送第二个视图控制器之前的属性呢? - Jarod
尝试使用我的方法与动态UILable,这只是一个可能对你有用的想法,伙计... :) - Paras Joshi
我看到了类似的情况,很好奇为什么会发生这种情况。 - Bradley Thomas
3个回答

1

使用这个动态的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;

希望这能帮到你...


如何让它只创建单元格而不重复使用?我没有看到任何方法可以做到这一点? - Jarod
有没有办法从这里进行更改?LocationList *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; - Jarod
@Jarod 刚刚把 dequeueReusableCellWithReuseIdentifier:nil 设置成了 nil,试试看吧。 - Paras Joshi
@Jarod,在cellForItemAtIndexPath方法中以编程方式添加标签,这样应该可以实现。 - Paras Joshi
也许你能回答这个问题,因为这样做会更容易。我为什么不能实现一个textView来代替一个标签,并将其放置在我想要的左上对齐位置。每当我在CollectionViewCell上放置一个textView时,我都会收到BAD_ACCESS异常。我认为这个对象只是被删除了,因为我正在使用ARC。 - Jarod
显示剩余11条评论

0

尝试使用这些代码:

contentLabel.textAlignment = UITextAlignmentLeft;
 contentLabel.lineBreakMode = UILineBreakModeWordWrap;

这两个函数在iOS 6中已经被弃用。 - Jarod
[contentLabel setNumberOfLines:5]; 试一下。 - Vishal
我有超过5行的代码,但是当我限制它时,它的行为是相同的。当我首次加载视图时,它是顶部对齐的,当我转到第二个视图控制器,然后返回到根视图控制器时,它们都居中对齐了。 - Jarod
我刚刚在Interface Builder中设置了框架。我应该在设置sizeToFit的m文件中设置它吗? - Jarod
你的意思是你不会通过编程方式创建标签吗? - Vishal
显示剩余6条评论

0

尝试这样做,并在.h文件中声明标签

contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 49, 202, 237)];
 [contentLabel setNumberOfLines:0];
    [contentLabel sizeToFit];
  [cell.contentLabel setText:tempString];

在 [cell.contentLabel setText:tempString]; 之后添加这行代码 [cell addSubview:contentLabel];。 - Vishal
cell.contentLabel自从您让我从Interface Builder中删除它以后,它不再是一个标签了。 - Jarod
是的,我确实通过编程创建了它,但它不再是 Cell 的属性,我无法设置。这是我目前拥有的,但仍然看不到文本: contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 49, 202, 237)]; [contentLabel setNumberOfLines:0]; [contentLabel sizeToFit]; [contentLabel setTextColor:[UIColor whiteColor]]; [contentLabel setText:tempString]; [cell.contentView addSubview:contentLabel]; return cell; - Jarod
在 -(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 方法中,看起来像那样的注释就这样做。 - Vishal
它仍然没有工作。我们能否解释一下为什么当我以另一种方式设置时它不能保持?你认为通过以编程方式添加标签会有任何不同的行为吗? - Jarod
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接