【问题标题】:UITextView inside UITableViewCellUITableViewCell 内的 UITextView
【发布时间】:2013-07-31 15:15:22
【问题描述】:

根据这个tutorial,我在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);
}

【问题讨论】:

  • 您是否在情节提要中制作此单元格,并且您是否使用自动布局?

标签: iphone ios objective-c uitableview uitextview


【解决方案1】:

在计算单元格的高度时,您需要考虑文本视图的填充。

如果您执行以下操作:

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

你会发现更多关于这个question的信息

【讨论】:

  • 谢谢,我用 UILabel 代替 UITextView 来解决这个问题。
【解决方案2】:

textview 内部有默认填充

使用 UILabel 代替 UITextview 或删除填充

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多