Проблема в выяснении многострочной способности UITableViewCell - PullRequest
1 голос
/ 01 июня 2011

С этим довольно далеко, но зависаю в той части, где я читаю строковую информацию.

Я создал ячейку, которая получает данные из внешнего 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 ]

1 голос
/ 01 июня 2011

Я бы предложил вам хранить в массиве ваши 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;
}
1 голос
/ 01 июня 2011
- (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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...