0

長いテキストを含むリスト ビューがあります。テキストが数段落の場合、途中で途切れてしまう傾向があります。奇妙な動作の一部は、常に同じ文字で切断されるわけではなく、一種のランダムであることです.

リストを作成するために使用するコードは次のとおりです。

// CREATING EACH CELL IN THE LIST
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    //static const NSInteger kLabelTag = 1;
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
            reuseIdentifier:@"business"];

    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
    NSString *first_name = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"first_name")];
    //Boolean *is_private = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"is_private")];

    // Creating a constraint size for the label you are making
    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

    // Determining the size that you will need for the label based on the comment
    // length and the contraint size
    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    //  Creating the label and initializing it with the frame that you have
    //  determined by your size calculations above
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, MAX(size.height, 44.0f) + 20.0f)];

    // setting up the label properties
    label.numberOfLines = 0;
    label.lineBreakMode = UILineBreakModeWordWrap;

    standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *is_private = [standardUserDefaults objectForKey:@"is_private"];

    if ( is_private == nil )
    {
        label.text = [first_name stringByAppendingString:[@": "  stringByAppendingString:comment]]; // comment;
    }
    else
    if ( [is_private isEqualToString:@"0"])
    {
        label.text = [first_name stringByAppendingString:[@": "  stringByAppendingString:comment]]; // comment;     
    }
    else 
    {
        label.text = comment;  
    }

    // adding the label view to the cell that you have created
    [cell.contentView addSubview:label];        

    // CLOSE THE SPINNER
    [spinner stopAnimating];

    // return the cell for the table view
    return cell;
}

//This method will determine how tall each row needs to be. Cell size for word wrapping.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{    
    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

    // Again you are getting the constraints because you are going to us this size
    // to constrain the height of the cell just like you determined the size for the label.
    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

    // Again, determining the size that we will need for the label, because it will drive
    // the height of the cell
    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    //  Finally, we can determine the height for the cell!
    CGFloat height = MAX(size.height, 44.0f);

    //  return the height, which is the height of the label plus some extra space to make
    //  it look good.
    return height + (10 * 2);
}

このコードに特に間違っている点はありますか? それとも、問題は別の場所に根ざしている可能性がありますか?

ありがとう!

4

2 に答える 2

1

どうしても必要な場合を除き、独自の UILabel を削除し、文字列のサイズに基づいてセルの高さを動的に調整します。以下の私の例をチェックしてください:

テーブルビューをロードする前に文字列を作成する

-(void)createStrings{

    newArray = [NSMutableArray array];

    for(NSDictionary *dictionary in items_array){
        NSString *comment = [dictionary objectForKey:(@"comment")];
        NSString *first_name = [dictionary objectForKey:(@"first_name")];
        NSString *combinedString = [first_name  stringByAppendingString:[@": "  stringByAppendingString:comment]];
        [newArray addObject:combinedString];
    }

}

#pragma mark - Table View Data Source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17];
    }

    cell.textLabel.text = [newArray objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - Table View Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17];
        CGSize constraintSize = CGSizeMake(self.tableView.frame.size.width, MAXFLOAT);
        CGSize labelSize = [[newArray objectAtIndex:indexPath.row] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

        return labelSize.height + 30;
}
于 2012-11-27T17:45:03.773 に答える
1

ラベルを作成するときは、最大の高さを渡します

MAX(size.height, 44.0f)

つまり、ラベルが 44 ポイントを超えることはありません。テキスト (ラベルの幅に合わせて折り返されている) がラベルよりも高い場合、その下にある行はラベルに表示されますが、表示されません。

あなたのオプション:

-ラベルのフォントをオートサイズにする

-高さを変更するか、ラベルの動的な高さに切り替えます

これは、更新するたびに次のようになります。

myLabel.text = @"some text";
CGRect rect = myLabel.frame;
rect.size.height = textView.contentSize.height;// Adding.size Since height is not a member of CGRect
textView.frame = rect; //your label is now as high as its contents (the text)

-ラベルに入るテキストをトリムして、ラベルの高さを超えないようにします。

于 2012-11-27T16:01:48.627 に答える