简体   繁体   English

iOS:在UITableViewCell中创建自调整大小的UITextView?

[英]IOS:Creating a Self-Sizing UITextView Within a UITableViewCell?

I am using stroyboards for my project. 我正在为项目使用滑板。 My project is based on parsing json datas and displaying it in JSON. 我的项目基于解析json数据并将其显示为JSON。 While parsing JSON I will receive two different datas. 解析JSON时,我将收到两个不同的数据。 Title and Description i have to parse this and have to fix in UItextview inside a uitableviewcell i have structured a uitableviewcell like this Title and Description我必须解析它,并且必须在UItextview inside a uitableviewcell修复,我已经像这样构造了uitableviewcell

在此处输入图片说明

TEXTVIEW,TEXTVIEW,LABEL,LABEL,LABEL,LABEL,LABEL; TEXTVIEW,TEXTVIEW,LABEL,LABEL,LABEL,LABEL,LABEL; Now my problem is my text view has to resize according to data, and based on that tableviewcell also has to resize I tried the above code but the view gets collapsing. 现在我的问题是我的文本视图必须根据数据调整大小,并且基于该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;

}

when i tried this code my screen looks like this 当我尝试此代码时,我的屏幕看起来像这样

在此处输入图片说明

Please help me to Solve this issue 请帮我解决这个问题

Check this link - http://www.springtiger.co.uk/2010/11/28/dynamically-generate-labels-frame-to-match-its-content/ 检查此链接-http: //www.springtiger.co.uk/2010/11/28/dynamically-generate-labels-frame-to-match-its-content/

It will explain how to resize UIlabel to fit contents. 它将说明如何调整UIlabel的大小以适合内容。

One more suggestion would be use a single control and use NSFormatted Text instead of so many labels. 另一个建议是使用单个控件并使用NSFormatted Text而不是太多标签。 (Again it depends on your requirement). (再次取决于您的要求)。

You need to move the labels' frame origin as well so that they are placed below each other. 您还需要移动标签的框架原点,以便将它们放置在彼此下方。 So after you calculate the first textView's size and bounds, set the origin of the second textView to whichever offset from the bottom of the first textView. 因此,在计算出第一个textView的大小和范围之后,将第二个textView的原点设置为与第一个textView底部的偏移量。 But that gets complicated when you have to take into account different orientations and device screen sizes... 但是,当您必须考虑不同的方向和设备屏幕尺寸时,这将变得很复杂。

Why don't you use auto-layout and constraints? 为什么不使用自动布局和约束?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin iOS如何在编辑时制作自定尺寸的UITableViewCell - Xamarin ios how to make a self-sizing UITableViewCell while editing 没有自动布局的自调整 UITableViewCell - Self-sizing UITableViewCell without Auto Layout 自调整UITableViewCell的内部带有UITableView的功能-自调整不起作用 - Self-sizing UITableViewCell with UITableView inside it - self-sizing doesn't work UITableViewCell 扩展 animation 与自定尺寸标签,可视 animation 问题 - UITableViewCell expanding animation with self-sizing labels, visual animation problem 自定义UITableViewCell永远不会以AutoLayout自调整大小显示 - Custom UITableViewCell never showing up with AutoLayout self-sizing 如何在具有自调整大小的单元格的 UITableViewCell 中添加填充? - How to add padding inside a UITableViewCell with self-sizing cells? 创建取决于UILabel高度的自定义表格视图单元格 - Creating Self-Sizing Table View Cells Dependant on UILabel Height iOS AutoLayout可自动调整整个表格的高度(不是单元格!) - iOS AutoLayout self-sizing whole table (not cells!) height iOS 10中的collectionViewContentSize使用自调整单元格 - collectionViewContentSize in iOS 10 using self-sizing cells iOS8使用Interface Builder自行调整静态TableView单元格 - iOS8 self-sizing static TableView cells with Interface Builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM