0

このチュートリアルによると、私UITextViewUITableViewCell. UITableViewがロードされたら、UITextViewと のUITableViewCell高さを変更します。

しかし、私はチュートリアルに基づいてすべてを行いました.UITextViewすべてのコンテンツを表示することはできません.コンテンツの一部はUITextViewスクロールして表示する必要があります.

これが私のコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *contentTableIdentify = @"contentTableIdentify";

    UITableViewCell *cell = nil;
//UILabel *label = nil;
    UITextView *textView = nil;
    cell = [self.tableView dequeueReusableCellWithIdentifier:contentTableIdentify];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contentTableIdentify];

        textView = [[UITextView alloc] initWithFrame:CGRectZero];
        [textView setTextColor:[UIColor blackColor]];
        [textView setFont:[UIFont fontWithName:@"Helvetica" size:12.0f]];
        [textView setBackgroundColor:[UIColor clearColor]];
        [textView setTag:1];
        [textView setEditable:NO];
        [[cell contentView] addSubview:textView];
    }

    NSString *text = [self.contentArray objectAtIndex:[indexPath row]];
    CGSize constraint = CGSizeMake(320 - (10 * 2), 99999.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    if (!textView)
        textView = (UITextView *)[cell viewWithTag:1];

    [textView setText:text];
    [textView setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [self.contentArray objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height, 44.0f);

    return height + (10 * 2);
}
4

2 に答える 2

0

セルの高さを計算するときは、テキスト ビューのパディングを考慮する必要があります。

次の場合:

//  Add 16px (8+8) to account fr the UITextView padding    
CGSize constraint = CGSizeMake(320 - (10 * 2) -16, 99999.0f);
GSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]

CGFloat height = MAX(size.height, 44.0f);

return height + (10 * 2) + (8*2);

この質問に関する詳細情報を見つけることができます

于 2013-07-31T15:45:54.830 に答える
0

テキストビューにはデフォルトのパディングがあります

ここに画像の説明を入力

UITextview の代わりに UILabel を使用するか、パディングを削除します

textView .contentInset = UIEdgeInsetsMake(-11,-8,0,0);
于 2013-07-31T17:02:28.517 に答える