Почему табличные ячейки не выпускаются? - PullRequest
0 голосов
/ 03 августа 2009

Я упростил свой код для тестирования, и все еще на телефоне мое использование памяти продолжает расти до такой степени, что таблица замедляется.

Может кто-нибудь сказать мне, что я здесь не так делаю?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell";
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
    }
    UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
    l.font=[UIFont boldSystemFontOfSize:15];
    l.textColor=[UIColor whiteColor];
    l.backgroundColor=[UIColor blackColor];
    l.text=@"Just some randoom text here";
    [cell.contentView addSubview:l];
    [l release];

К сожалению. Эта вставка кода не сработала. Вот прямая паста:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell";
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
    }
    UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
    l.font=[UIFont boldSystemFontOfSize:15];
    l.textColor=[UIColor whiteColor];
    l.backgroundColor=[UIColor blackColor];
    l.text=@"Just some randoom text here";
    [cell.contentView addSubview:l];
    [l release];
        return cell;
}

Ответы [ 3 ]

1 голос
/ 04 августа 2009

Вы хотите следовать шаблону, подобному этому:

#define kTagMyLabel 1

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
  static NSString *cellID = @"Cell1";
  UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
  UILabel * l;
  if (cell == nil) {
    // create the cell
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];

    // perform setup/functions that are common to all cells
    l = [[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)] autorelease];
    l.font=[UIFont boldSystemFontOfSize:15];
    l.textColor=[UIColor whiteColor];
    l.backgroundColor=[UIColor blackColor];
    l.tag = kTagMyLabel ;
    [cell.contentView addSubview:l];
  }
  else
  {
    // find the label we previously added.
    l = (UILabel*)[cell viewWithTag:kTagMyLabel];
  }

  // now set up the cell specific to this indexPath
  l.text=@"Just some random text here";
  return cell;
}
0 голосов
/ 04 августа 2009

Поскольку каждый UITableViewCell имеет свой собственный стандартный UILabel (cell.textLabel), вам действительно нужна дополнительная метка, добавленная в contentView? Если вам нужны пользовательские ячейки, вы можете рассмотреть возможность создания подкласса UITableViewCell.

0 голосов
/ 03 августа 2009

Вы перерабатываете экземпляры UITableViewCell, но по-прежнему создаете новый экземпляр UILabel для каждой строки и добавляете его в каждую ячейку. Отсюда и использование памяти. Попробуйте что-то вроде этого:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell";
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
        UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
        l.font=[UIFont boldSystemFontOfSize:15];
        l.textColor=[UIColor whiteColor];
        l.backgroundColor=[UIColor blackColor];
        l.text=@"Just some randoom text here";
        [cell.contentView addSubview:l];
        [l release];
    }
    return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...