Как должен я addSubview к cell.contentView?

(Когда ячейка недавно создается):

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frame = CGRectMake(0, 0, 160, 50);
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentRight;
        label.text = @"9:00am";
        [cell.contentView addSubview:label];
        [label release];
    }

    return cell;
}

или B (каждый раз, когда ячейка найдена):

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

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

    CGRect frame = CGRectMake(0, 0, 160, 50);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = UITextAlignmentRight;
    label.text = @"9:00am";
    [cell.contentView addSubview:label];
    [label release];

    return cell;
}

A или B?Спасибо!

Решение для ОБНОВЛЕНИЯ (благодарит за ответы):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";    
    UILabel *label;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frame = CGRectMake(0, 0, 160, 50);
        label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentRight;
        label.tag = 1;
        [cell.contentView addSubview:label];
        [label release];
    } else {
        label = (UILabel *) [cell viewWithTag:1];
    }

    label.text = [NSString stringWithFormat:@"%d", [indexPath row]];

    return cell;
}
25
задан ohho 16 August 2010 в 06:01
поделиться

2 ответа

Все дело в производительности. С помощью A вы повторно используете ячейку со всеми ее подпредставлениями, с помощью B вы повторно используете только необработанную ячейку и добавляете новое подпредставление на каждой итерации, что, IMHO, не так хорошо, как A re: performance.

Я предлагаю либо создать подкласс UITableView , либо использовать решение A.

11
ответ дан 28 November 2019 в 21:52
поделиться

Вы должны добавлять подвиды только при создании ячейки, как в A, однако каждый раз присваивайте значения меткам и т. Д., Как в B.

Это решение выйдет из строя естественным образом, если вы создадите свой собственный подкласс UITableViewCell который добавляет свои собственные подвиды.

Что-то вроде этого.

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frame = CGRectMake(0, 0, 160, 50);
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentRight;
        [cell.contentView addSubview:label];
        [label release];
    }

    // Get a reference to the label here

    label.text = @"9:00am";

    return cell;
}

Таким образом, вы получаете преимущества в производительности, выделяя подвид только один раз, и вы можете просто установить соответствующие свойства в подвиде по мере необходимости.

11
ответ дан 28 November 2019 в 21:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: