2013-03-02 4 views
1

Я добавляю два пользовательских UILabel в разделе моего UITableView таким образом:UITableViewCell и UILabel

//in .h file: 
NSArray *listaopzioni; 
@property (nonatomic, retain) NSArray *listaopzioni; 

//in .m file: 
self.listaopzioni = [[NSArray arrayWithObjects:@"Strumenti",@"Help & Credits", nil] retain]; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if ([indexPath section]==0) { 

     cell.accessoryType = UITableViewCellAccessoryNone; 

     UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)]; 
     slogan.text=[listaopzioni objectAtIndex:indexPath.row]; 
     slogan.textAlignment=UITextAlignmentCenter; 
     slogan.font= [UIFont boldSystemFontOfSize:20]; 
     slogan.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:slogan]; 
     [slogan release]; 


    } 
} 

Все ковшики отлично, но когда я скользить вверх и вниз по Tableview (пытаясь скрыть ячейки ниже UINavigationBar) Я получаю странный эффект: текст перекрывается, просто делая каждую букву толще.

Что случилось?

ответ

6

Метод cellForRowAtIndexPath вызывается каждый раз, когда ячейка становится видимой. Именно поэтому он создает метки каждый раз, когда вы прокручиваете. Решения поставить создание ярлыка при создании ячейки:

if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

    if ([indexPath section]==0) { 

    cell.accessoryType = UITableViewCellAccessoryNone; 

    UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)]; 
    slogan.text=[listaopzioni objectAtIndex:indexPath.row]; 
    slogan.textAlignment=UITextAlignmentCenter; 
    slogan.font= [UIFont boldSystemFontOfSize:20]; 
    slogan.backgroundColor=[UIColor clearColor]; 
    [cell.contentView addSubview:slogan]; 
    [slogan release]; 


    } 
} 
+0

Большое спасибо! – SirSeymour

1

UITableViewCells (при правильном использовании) получить повторно, что означает, после того, как они были созданы, они сохраняют созданное состояние, т.е. метки была добавлен в вашу камеру. Что вам нужно сделать, так это использовать повторное использование этой ячейки в ваших интересах:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UILabel slogan; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     slogan = [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)]; 
     slogan.tag = 2121; // Any unique-to-the-cell, positive integer 
     slogan.textAlignment=UITextAlignmentCenter; 
     slogan.font= [UIFont boldSystemFontOfSize:20]; 
     slogan.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:slogan]; 
    } else { 
     slogan = [cell viewWithTag:2121]; // Must match slogan.tag 
    } 

    if ([indexPath section]==0) { 

     cell.accessoryType = UITableViewCellAccessoryNone; 

     slogan.text=[listaopzioni objectAtIndex:indexPath.row]; 

    } 
} 
Смежные вопросы