IOS:在UITableViewCell中创建自定义大小的UITextView?

时间:2014-02-27 11:41:29

标签: ios iphone objective-c uitableview

我正在为我的项目使用stroyboards。我的项目基于解析json数据并以JSON格式显示。在解析JSON时,我将收到两个不同的数据。 Title and Description 我必须解析这个并且必须修复UItextview inside a uitableviewcell我构建了一个像这样的uitableviewcell

enter image description here

的TextView,TextView的,LABEL,LABEL,LABEL,LABEL,LABEL; 现在我的问题是我的文本视图必须根据数据调整大小,并且基于该tableviewcell还必须调整大小我尝试了上面的代码,但视图会崩溃。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Need_Request";
    NeedReqListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    type = (NSString *)[array4 objectAtIndex:indexPath.row];
    if([type isEqualToString:@"Money Donation"]) {
        cell.createdby.text = (NSString *)[array3 objectAtIndex:indexPath.row];
        cell.product.text = (NSString *)[array4 objectAtIndex:indexPath.row];
        cell.expiration.text = (NSString *)[array5 objectAtIndex:indexPath.row];
        cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
        [cell.descview setHidden:TRUE];
        [cell.totalquantity setHidden:TRUE];
        [cell.fulfilledquantity setHidden:TRUE];
        return cell;

    }
    else if([type isEqualToString:@"Service"]) {
        cell.createdby.text = (NSString *)[array3 objectAtIndex:indexPath.row];
        cell.product.text = (NSString *)[array4 objectAtIndex:indexPath.row];
        cell.expiration.text = (NSString *)[array5 objectAtIndex:indexPath.row];
        cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
        cell.descview.text = (NSString *)[array2 objectAtIndex:indexPath.row];
        [cell.totalquantity setHidden:TRUE];
        [cell.fulfilledquantity setHidden:TRUE];
        return cell;

    }

    cell.createdby.text = (NSString *)[array3 objectAtIndex:indexPath.row];
    cell.product.text = (NSString *)[array4 objectAtIndex:indexPath.row];
    cell.expiration.text = (NSString *)[array5 objectAtIndex:indexPath.row];
    cell.totalquantity.text = (NSString *)[array6 objectAtIndex:indexPath.row];
    cell.fulfilledquantity.text = (NSString *)[array7 objectAtIndex:indexPath.row];
    cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
    cell.descview.text = (NSString *)[array2 objectAtIndex:indexPath.row];
    CGRect bodyFrame = cell.titleview.frame;
    bodyFrame.size = cell.titleview.contentSize;
    [cell.titleview setFrame:bodyFrame];
    CGRect bodyFrame1 = cell.descview.frame;
    bodyFrame1.size = cell.descview.contentSize;
    [cell.descview setFrame:bodyFrame1];
    CGRect bodyFrame2 = cell.createdby.frame;
    [cell.createdby setFrame:bodyFrame2];
    CGRect bodyFrame3 = cell.product.frame;
    [cell.product setFrame:bodyFrame3];
    CGRect bodyFrame4 = cell.expiration.frame;
    [cell.expiration setFrame:bodyFrame4];
    CGRect bodyFrame5 = cell.totalquantity.frame;
    [cell.totalquantity setFrame:bodyFrame5];
    CGRect bodyFrame6 = cell.fulfilledquantity.frame;    
     [cell.fulfilledquantity setFrame:bodyFrame6];


    return cell;

}

当我尝试使用此代码时,我的屏幕看起来像这样

enter image description here

请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

点击此链接 - http://www.springtiger.co.uk/2010/11/28/dynamically-generate-labels-frame-to-match-its-content/

它将解释如何调整UIlabel的大小以适应内容。

还有一个建议是使用单个控件并使用NSFormatted Text而不是那么多标签。 (这又取决于你的要求)。

答案 1 :(得分:0)

您还需要移动标签的框架原点,以便将它们放在彼此下方。因此,在计算第一个textView的大小和边界后,将第二个textView的原点设置为与第一个textView底部的偏移量。但是,当您必须考虑不同的方向和设备屏幕尺寸时,这会变得复杂......

为什么不使用自动布局和约束?

相关问题