ios根据uitableviewcell中的数据更改textview大小?

时间:2014-02-13 10:45:02

标签: ios objective-c uitableview

我的项目基于解析来自webservice的json数据,并在屏幕中显示。现在我的问题,我收到像这样的json数据

needList =     (
                {
            createdBy = "Created By: u,u1";
            expDate = "Expiration Date: 27/Feb/2014";
            fulfillQuan = null;
            id = 630;
            needDesc = "prayer description";
            needFrom = need;
            needTitle = "prayer title";
            needType = Service;
            totalQuan = null;
        },
                {
            createdBy = "Created By: u,u1";
            expDate = "Expiration Date: 27/Feb/2014";
            fulfillQuan = "FulfilledQuantity: 0";
            id = 629;
            needDesc = food;
            needFrom = need;
            needTitle = "food title";
            needType = Product;
            totalQuan = "TotalQuantity:10";
        },
                {
            createdBy = "Created By: u3,u3";
            expDate = "Expiration Date: 26/Feb/2014";
            fulfillQuan = "FulfilledQuantity: 0";
            id = 640;
            needDesc = "Food Description";
            needFrom = need;
            needTitle = "Food Title";
            needType = "Food Donation";
            totalQuan = "TotalQuantity:10";
        },
                {
            createdBy = "Created By: u,u1";
            expDate = "Expiration Date: 25/Feb/2014";
            fulfillQuan = "FulfilledQuantity: 0";
            id = 635;
            needDesc = "cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,cell phone description ,";
            needFrom = need;
            needTitle = "cell title blackberry, cell title blackberry,cell title blackberry,cell title blackberry,cell title blackberry,cell title blackberry,cell title blackberry,cell title blackberry,cell title blackberry,cell title blackberry,cell title blackber";
            needType = Product;
            totalQuan = "TotalQuantity:10";
        },
                {
            createdBy = "Created By: c,c31";
            expDate = "Expiration Date: 20/Feb/2014";
            fulfillQuan = "FulfilledQuantity: 0";
            id = 638;
            needDesc = null;
            needFrom = need;
            needTitle = "test money doantion";
            needType = "Money Donation";
            totalQuan = "TotalAmount :2000";
        },
                {
            createdBy = "Created By: c,c31";
            expDate = "Expiration Date: 20/Feb/2014";
            fulfillQuan = "FulfilledQuantity: 0";
            id = 639;
            needDesc = null;
            needFrom = need;
            needTitle = "need money title";
            needType = "Money Donation";
            totalQuan = "TotalAmount :55";
        }
    );
}

我收到的这两个数据导致我遇到问题。 needDescneedTitle。是textviewtableviewcell(subclassed)内的那些数据。现在我需要根据收到的数据自动调整textview的大小。我尝试过,但没有得到适当的解决方案。我不知道该怎么做。请帮助我的朋友。提前致谢 这是我的tableview单元格的代码和我尝试的调整代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Need_Request";
    NeedReqListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    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];

    CGRect bodyFrame = cell.titleview.frame;
    bodyFrame.size = cell.titleview.contentSize;
    [cell.titleview setFrame:bodyFrame];

    return cell;

}

以下是我为自动调整大小而尝试的代码但是没有用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NeedReqListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Need_Request"];
    float oldHeight = cell.titleview.frame.size.height;
    cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
    CGRect bodyFrame = cell.titleview.frame;
    bodyFrame.size = cell.titleview.contentSize;
    float newHeight = cell.titleview.contentSize.height;
    return tableView.rowHeight - oldHeight + newHeight;
}

5 个答案:

答案 0 :(得分:0)

尝试以下代码..

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {

             NSString *cellText1=[array2 objectAtIndex:indexPath.row];;
             UIFont *cellFont1=[UIFont fontWithName:@"SourceSansPro-Bold" size:18];
             CGSize constraintSize1=CGSizeMake(300,MAXFLOAT);
             CGSize txtViewSize1=[cellText1 sizeWithFont:cellFont1 constrainedToSize:constraintSize1 lineBreakMode:NSLineBreakByWordWrapping];

            NSString *cellText=[array1 objectAtIndex:indexPath.row]; //if it returns yourtextview text
            UIFont *cellFont = [UIFont fontWithName:@"SourceSansPro-Regular" size:13.0];
            CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);//225
            CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

            return labelSize.height+txtViewSize1.height+150; //here +60 is the total height of other fields which are displayed in your custom cell
    }

希望有所帮助......

答案 1 :(得分:0)

试试这个:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {    
NSString *text = [self.arrTexts objectAtIndex:indexPath.section];
    CGSize size;
    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        // code here for iOS 5.0,6.0 and so on
         CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]];
         size = fontSize;
    } else {
        // code here for iOS 7.0
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont fontWithName:@"Helvetica" size:17], NSFontAttributeName,
                                              nil];
       CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(605, 500)
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:attributesDictionary
                                           context:nil];
        size = fontSizeFor7.size;
    }
    return size.height +15;
}

答案 2 :(得分:0)

Calculate height like this:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
            NSString *String=[array1 objectAtIndex:indexPath.row]; 
            CGSize constraintSize = CGSizeMake(titleviewWidth, FLT_MAX);
            CGSize size = [cellText sizeWithFont:titleviewFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
            return size.height+padding; // add extra padding height
  }

Change titleview frame like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   .........
   .........
   /* 
CGRect bodyFrame = cell.titleview.frame;
    bodyFrame.size = cell.titleview.contentSize;
    // it not possible change titleview frame in this class
    [cell.titleview setFrame:bodyFrame];
*/
   //add method and change frame in NeedReqListCell class
   [cell changeHeight:titleviewHeight];

return cell;
}

答案 3 :(得分:0)

//Add Every Label like This., in cellForRowAtIndexPath method

    NSString *text = [NSString stringWithFormat:@"%@",[[Array objectAtIndex:indexPath.row] objectForKey:@"needDesc"]];
    UIFont *font = [UIFont fontWithName:@"ArialMT" size:12];
    CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];
    UILabel *lbl_desc=[[UILabel alloc]init];
    lbl_desc.numberOfLines = 0;
    lbl_desc.frame=CGRectMake(70,18, size.width, size.height);
    lbl_desc.lineBreakMode = NSLineBreakByWordWrapping;
    lbl_desc.text = (text ? text : @"");
    lbl_desc.font = font;
    lbl_desc.backgroundColor=[UIColor clearColor];
    lbl_desc.textColor = [UIColor darkTextColor];
    [cell.contentView addSubview:lbl_desc];
    [lbl_desc release];

答案 4 :(得分:0)

//For Height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [NSString stringWithFormat:@"%@",[[Array objectAtIndex:indexPath.row] objectForKey:@"needDesc"]];
    UIFont *font = [UIFont fontWithName:@"ArialMT" size:12];
    CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];


    NSString *secondtext = [NSString stringWithFormat:@"%@",[[Array objectAtIndex:indexPath.row] objectForKey:@"needDescsecond"]];
    UIFont *secondfont = [UIFont fontWithName:@"ArialMT" size:12];
    CGSize secondsize = [(secondtext ? secondtext : @"") sizeWithFont:secondfont constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];

    int last_size = size.height +secondsize.height + 140;

    return last_size;
}