簡體   English   中英

UITableViewCell內的UITextView

[英]UITextView inside UITableViewCell

根據本教程 ,我在UITableViewCell有一個UITextView 加載UITableView ,我希望UITextViewUITableViewCell調整其高度。

但是我根據教程做了所有事情, 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);
}

在計算單元格的高度時,您需要考慮文本視圖的填充。

如果您執行以下操作:

//  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);

您將找到有關此問題的更多信息

textview內部具有默認填充

在此處輸入圖片說明

使用UILabel代替UITextview或刪除填充

textView .contentInset = UIEdgeInsetsMake(-11,-8,0,0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM