повторное использование подкласса UITableViewCell - PullRequest
1 голос
/ 21 марта 2012

Я пытаюсь создать подкласс UITableViewCell, который просто содержит свойство UILabel "nameLabel" и свойство UILabel "statusLabel". Для правильного удаления ячеек я реализовал метод cellForRowAtIndexPath: this:

PS: текст statusLabel взят из Twitter API, поэтому я должен отправить его размер [[UILabel alloc] initWithFrame:]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"StatusCellView";
    StatusCellView *cell = [tableView dequeueReusableCellWithIdentifier:
                        CellIdentifier];
    if (cell == nil) {
        cell = [[StatusCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    if (![cell viewWithTag:kNameLabelTag]) {
        //init the nameLabel and [cell.contentView addsubview:nameLabel]
    }
    if (![cell viewWithTag:kStatusLabelTag]) {
        //init the statusLabel and [cell.contentView addsubview:statusLabel]
    }

    return cell;

Как видите, это хорошо работает, когда данные не так велики.Но мой вопрос:

  • Я думаю, что повторное удаление и добавление подпредставления ухудшит производительность.Это грубый способ ...
  • Я могу реализовать ту же функцию без создания подкласса UITableViewCell.Есть ли способ использовать подкласс?

1 Ответ

0 голосов
/ 21 марта 2012

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"StatusCellView";
StatusCellView *cell = [tableView dequeueReusableCellWithIdentifier:
                    CellIdentifier];
if (cell == nil) {
    cell = [[StatusCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //init the nameLabel and [cell.contentView addSubview:nameLabel];
    //init the statusLabel and [cell.contentView addsubview:statusLabel]
}
else {
    UILabel *nameLabel = [cell viewithTag:kStatusLabelTag];
    nameLabel.frame = newFrame;
    nameLabel.text = newText
    //etc, etc, etc
}
return cell;
...