3

したがって、私の rootViewController にはカスタム UICollectionViewCell を持つ UICollectionView が含まれています。変更してはならないヘッダーのセルにラベルがあり、残りのセルの本文を構成する複数行のラベルがあります。アプリを起動すると、sizeToFit が正しく呼び出され、すべてが上に配置されるため、中央に配置されません。セルの 1 つをクリックして次のビューに移動し、[戻る] ボタンをクリックして rootViewController に戻ると、viewWillAppear メソッドが実行されてデータがリロードされますが、sizeToFit が機能せず、複数行のすべてが表示されますラベルが中央揃えになります。それはまだ複数の行ですが、数行しかない場合、ラベルの中央に位置し、見栄えがよくありません. 一貫性があるようにこれを維持するにはどうすればよいですか。ラベルを再度正しく配置する rootViewController をリロードする左側のメニューがありますが、secondViewController から戻るボタンを押すと、整列しなくなります。すべてのセルをクリアしてリロードする方法はありますか。[collectionView reloadData]; を試しました。viewWillAppear で機能しません。現在、ネットワーク接続が viewWillAppear メソッドから呼び出される connectionDidFinish の最後に呼び出されます。任意の支援をいただければ幸いです。現在、ネットワーク接続が viewWillAppear メソッドから呼び出される connectionDidFinish の最後に呼び出されます。任意の支援をいただければ幸いです。現在、ネットワーク接続が viewWillAppear メソッドから呼び出される connectionDidFinish の最後に呼び出されます。任意の支援をいただければ幸いです。

これはカスタム 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;
}
4

3 に答える 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;

これがあなたを助けることを願っています...

于 2012-12-20T05:05:13.073 に答える
0

次の行を試してください。

contentLabel.textAlignment = UITextAlignmentLeft;
 contentLabel.lineBreakMode = UILineBreakModeWordWrap;
于 2012-12-20T03:48:33.737 に答える
0

そのようにして、.hファイルでラベルを宣言します

contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 49, 202, 237)];
 [contentLabel setNumberOfLines:0];
    [contentLabel sizeToFit];
  [cell.contentLabel setText:tempString];
于 2012-12-20T04:24:09.323 に答える