Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何根据行内容的不同制作可更改的表格高度大小?_Iphone_Ios_Xcode - Fatal编程技术网

Iphone 如何根据行内容的不同制作可更改的表格高度大小?

Iphone 如何根据行内容的不同制作可更改的表格高度大小?,iphone,ios,xcode,Iphone,Ios,Xcode,我希望行高度基于内容高度。。。因此,根据内容调整每行高度的大小 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return tableView.contentSize.height; } 这是我到目前为止所做的,但它不起作用。是的,因为调用此方法时,单元格的内容视图的帧为CGRectZero。您最好为单元格指定一个自定义属性并返回该属性。基

我希望行高度基于内容高度。。。因此,根据内容调整每行高度的大小

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return tableView.contentSize.height;
}

这是我到目前为止所做的,但它不起作用。

是的,因为调用此方法时,单元格的内容视图的帧为CGRectZero。您最好为单元格指定一个自定义属性并返回该属性。

基本上,您需要获取单元格的内容,然后根据该内容确定单元格的大小。例如,您可能有

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSString *string = cell.textLabel.text;
    CGSize textSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:15] constrainedToSize:CGSizeMake(240, 5000)];
    return textSize.height + 40;
}    
上面的操作是获取单元格中的文本,计算所需的高度(如果宽度为240px,使用Helvetica 15pt),然后相应地更改单元格的高度。您仍然需要在
cellforrowatinexpath
方法中调整
单元格.textlab
的边框

Try this code for exmaple and also refer this http://www.icodeblog.com/2010/11/18/making-smarter-table-view-cells/  

 #define FONT_SIZE 14.0f
    #define CELL_CONTENT_WIDTH 125.0f
    #define CELL_CONTENT_MARGIN 10.0f
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       // static NSString *CellIdentifier = @"Cell";
        NSString *CellIdentifier = [NSString stringWithFormat:@"%d", indexPath.row] ;
        UILabel *lblTitle = nil;
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;




            lblTitle =[[UILabel alloc]initWithFrame:CGRectMake(5, 15, 110, 44)];
            lblTitle.backgroundColor = [UIColor clearColor];
            lblTitle.textColor = [UIColor whiteColor];
            lblTitle.font = [UIFont systemFontOfSize:11.0];
            lblTitle.tag = 1;
            lblTitle.numberOfLines = 0;


            UILabel *lblType =[[UILabel alloc]initWithFrame:CGRectMake(125, 0, 75, 44)];
            lblType.backgroundColor = [UIColor clearColor];
            lblType.textColor = [UIColor whiteColor];
            lblType.font = [UIFont systemFontOfSize:11.0];
            lblType.tag = 2;

            UILabel *lblDate =[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 60, 44)];
            lblDate.backgroundColor = [UIColor clearColor];
            lblDate.textColor = [UIColor whiteColor];
            lblDate.font = [UIFont systemFontOfSize:11.0];
            lblDate.tag = 3;

            UILabel *lblTime =[[UILabel alloc]initWithFrame:CGRectMake(265, 0, 50, 44)];
            lblTime.backgroundColor = [UIColor clearColor];
            lblTime.textColor = [UIColor whiteColor];
            lblTime.font = [UIFont systemFontOfSize:11.0];
            lblTime.tag = 4;

            [tableView beginUpdates];
            [tableView endUpdates]; 
            [cell.contentView addSubview:lblTitle];
            [cell.contentView addSubview:lblType];
            [cell.contentView addSubview:lblDate];
            [cell.contentView addSubview:lblTime];

        }

    //    lblTitle = (UILabel *) [cell.contentView viewWithTag:1];
    //    lblTitle.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"];
    //    NSString *strTitle =[NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];


    //    lblTitle.text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];

        NSString *text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];


        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeCharacterWrap];

        if (!lblTitle)
            lblTitle = (UILabel*)[cell viewWithTag:1];

        [lblTitle setText:text];
        [lblTitle setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];


        UILabel *lblType = (UILabel *) [cell.contentView viewWithTag:2];
        lblType.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"type"];

        UILabel *lblDate = (UILabel *) [cell.contentView viewWithTag:3];
        lblDate.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"date"];

        UILabel *lblTime = (UILabel *) [cell.contentView viewWithTag:4];
        lblTime.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"time"];

        return cell;
    }
    -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeCharacterWrap];

        CGFloat height = MAX(size.height, 44.0f);

        return height + (CELL_CONTENT_MARGIN * 2);
    }