麻烦搞清楚UITableViewCell多线能力

时间:2011-06-01 10:07:21

标签: iphone dynamic uitableview multiline

与这个相距甚远,但我正在读出我读出字符串信息的部分。

我创建了一个从外部xml文件接收数据的单元格,它一切正常,但有些单元格包含很多文本,我想在多行显示。也没问题。但棘手的部分是我的细胞的动态高度。我在heightForRowAtIndexPath:方法中配置了这个,但是我需要知道单元格包含的文本(行)的数量,并且我仍然坚持如何将它连接到我的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;
}

2 个答案:

答案 0 :(得分:1)

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

CGSize maxSize = CGSizeMake(urMaxSize);
CGSize cellSize = [itemName sizeWithFont:[UIFont systemFontOfSize:15]
         constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
return cellSize.height;
}

itemName是您要填写的文字。我猜它是[infoTitleArray objectAtIndex:indexPath.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;
}

答案 1 :(得分:1)

我建议您将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;
}