Iphone 无法确定UITableViewCell的多行功能

Iphone 无法确定UITableViewCell的多行功能,iphone,dynamic,uitableview,multiline,Iphone,Dynamic,Uitableview,Multiline,这篇文章讲得很深入,但我仍在读字符串信息的部分 我制作了一个从外部xml文件接收数据的单元格,它可以正常工作,但是有些单元格包含了太多的文本,我想在多行上显示这些文本。也没问题。但棘手的是我手机的动态高度。我在heightforrowatinexpath:方法中配置了这一点,但是我需要知道单元格包含的文本(行)的数量,我一直在研究如何将其连接到我的cellText(string)变量。 欢迎任何帮助:-) 这是我的密码: - (UITableViewCell *)tableView:(UITab

这篇文章讲得很深入,但我仍在读字符串信息的部分

我制作了一个从外部xml文件接收数据的单元格,它可以正常工作,但是有些单元格包含了太多的文本,我想在多行上显示这些文本。也没问题。但棘手的是我手机的动态高度。我在heightforrowatinexpath:方法中配置了这一点,但是我需要知道单元格包含的文本(行)的数量,我一直在研究如何将其连接到我的cellText(string)变量。 欢迎任何帮助:-)

这是我的密码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

    // Set the text in the cell for the section/row.
    NSString *endDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.end]];
    int endDateLength = endDate.length;
    NSString *endTime = [NSString stringWithFormat:@"%@", [endDate substringFromIndex:endDateLength -7]];

    NSString *startDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]];
    int startDateLength = startDate.length;
    NSString *startTime = [NSString stringWithFormat:@"%@", [startDate substringFromIndex:startDateLength -7]];

    NSString *date = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]];
    int dateLength = date.length;
    NSString *dateString = [NSString stringWithFormat:@"%@", [date substringToIndex:dateLength -7]];

    NSString *cellText = nil;
    NSString *cellExplainText = nil;

    //Pre title arrays
    dateTitleArray      = [[NSArray alloc] initWithObjects:@"Dag", @"Start tijd", @"Eind tijd", nil];
    nameTitleArray      = [[NSArray alloc] initWithObjects:@"Naam", @"Graad",nil];
    addressTitleArray   = [[NSArray alloc] initWithObjects:@"Dojo", @"Straat", @"Plaats",nil];
    infoTitleArray      = [[NSArray alloc] initWithObjects:@"Kosten", @"Contact", @"Details", nil];

    dateArray           = [[NSArray alloc] initWithObjects: dateString, startTime, endTime, nil];
    nameArray           = [[NSArray alloc] initWithObjects: stage.teacher, stage.grade, nil];
    addressArray        = [[NSArray alloc] initWithObjects: stage.dojo, stage.street, stage.city, nil];
    infoArray           = [[NSArray alloc] initWithObjects: stage.cost, stage.contact, stage.details, nil];

    switch (indexPath.section)
    {
        case 0:
            cellExplainText = [dateTitleArray objectAtIndex:indexPath.row];
            cellText        = [dateArray objectAtIndex:indexPath.row];
            break;
        case 1:
            cellExplainText = [nameTitleArray objectAtIndex:indexPath.row];
            cellText        = [nameArray objectAtIndex:indexPath.row];
            break;
        case 2:
            cellExplainText = [addressTitleArray objectAtIndex:indexPath.row];
            cellText        = [addressArray objectAtIndex:indexPath.row];
            break;
        case 3:
            cellExplainText = [infoTitleArray objectAtIndex:indexPath.row];
            cellText        = [infoArray objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }

    [dateTitleArray release];
    [nameTitleArray release];
    [addressTitleArray release];
    [infoTitleArray release];

    [dateArray release];
    [nameArray release];
    [addressArray release];
    [infoArray release];

    cell.textLabel.text = cellExplainText;
    cell.detailTextLabel.text = cellText;
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.detailTextLabel.numberOfLines = 0;
    cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellTextSize = ????????????;
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 12;
}
itemName
是您要填充的文本。我猜是
[infoTitleArray objectAtIndex:indexath.row]
和基于索引的相应数组信息。您也可以在此方法中获取节,以便获取字符串

如果你想用你的方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText;
switch (indexPath.section)
    {
        case 0:
            cellText = [dateTitleArray objectAtIndex:indexPath.row];

            break;
        case 1:
            cellText = [nameTitleArray objectAtIndex:indexPath.row];

            break;
        case 2:
            cellText = [addressTitleArray objectAtIndex:indexPath.row];

            break;
        case 3:
            cellText = [infoTitleArray objectAtIndex:indexPath.row];

            break;
        default:
            break;
    }

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 12;
}

我建议您将
cellText
/
cellTextExplained
值存储在一个数组中,以便在
heightForRowAtIndexPath:
中可以检索它们:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
     NSString *cellText = [self.cellTextArray:objectAtIndex:indexPath.row];

     //-- rest of your code here
     UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
     CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
     CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 12;
}

嗨,谢谢你迄今为止的帮助。我实现了你提供给我的代码。上面的文本块。但是细胞的动态高度没有改变。我仍然看到3行或更多的文本被压缩到标准大小的单元格中。它不考虑行的数量。有什么建议吗?[code](NSString*cellText;switch(indexath.section){case 0:cellText=[dateTitleArray objectAtIndex:indexath.row];break;case 1:cellText=[nameTitleArray objectAtIndex:indexath.row];break;case 3:cellText=[infoTitleArray objectAtIndex:indexath.row];break;默认值:break;}).[代码](UIFont*cellFont=[UIFont fontWithName:@“Helvetica”大小:14.0];CGSize constraintSize=CGSizeMake(280.0f,MAXFLOAT);CGSize标签大小=[cellText sizeWithFont:cellFont ConstraintedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];返回labelSize.height+12;)当我在开关函数中执行NSLog检查时,它会显示(null)每种情况。不知何故,它无法检索数据。请尝试分配字符串并进行检查。请确保在需要时释放它。抱歉,这可能是一个非常新的问题。我正在尝试将cellText/cellTextExplained放入数组,但我现在必须有一个写入程序块。您建议我如何执行此操作?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
     NSString *cellText = [self.cellTextArray:objectAtIndex:indexPath.row];

     //-- rest of your code here
     UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
     CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
     CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 12;
}