2013-08-26 5 views
-2

как создавать данные в таблицеViewCell при прокрутке не накапливаться?данные в табличном виде накапливаются при прокрутке

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

    static NSString *theCell = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theCell]; 

    if (! cell) { 

     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theCell]; 
    } 

    UILabel *data1 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]WithFrame:CGRectMake(10, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data1 atIndex:0]; 

    UILabel *data2 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"subjek"]]WithFrame:CGRectMake(50, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data2 atIndex:1]; 

    UILabel *data3 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"shadowScore"]]WithFrame:CGRectMake(200, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data3 atIndex:2]; 

    UILabel *data4 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"finalScore"]]WithFrame:CGRectMake(250, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data4 atIndex:3]; 

    UILabel *data5 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"kkm"]]WithFrame:CGRectMake(300, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data5 atIndex:4]; 

    UILabel *data6 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"Status"]]WithFrame:CGRectMake(350, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data6 atIndex:5]; 

    UILabel *data7 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"teachernote"]]WithFrame:CGRectMake(400, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
    [cell insertSubview:data7 atIndex:6]; 

    return cell; 
} 
+0

Можете ли вы разместить некоторые изображения того, что в настоящее время производится, и что вы хотите? –

+0

Как задать вопрос, чтобы люди действительно знали, что ваша проблема, и * может * помочь вам? –

ответ

6

Проблема каждый раз клетка загружается, вы добавляете новый UILabel. Это становится проблемой, когда ячейки перерабатываются, поскольку у них уже есть метки, и вы создаете больше.

Вы должны быть подклассифицированы UITableViewCell, создавая UILabel s и макет, который вы хотите загрузить, а затем просто устанавливаете информацию в методе cellForRowAtIndexPath:. Это позволяет вам перерабатывать ячейки и улучшать производительность при сохранении макетов и избегать этой «проблемы стекирования», которую вы нашли.

Второй вариант

Как второй, менее желательны, опция, чтобы переместить UILabel создать методы в if (!cell) блока и извлечения их с помощью тегов и устанавливать их вне этого блока. Это менее портативный и более хрупкий, однако такой же эффективный эффект повторного использования меток будет на месте. Это выглядело бы примерно так:

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

    static NSString *theCell = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theCell]; 

    if (! cell) { 

     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theCell]; 

     UILabel *data1 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]WithFrame:CGRectMake(10, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]]; 
     data1.tag = 1; 
     [cell insertSubview:data1 atIndex:0]; 

     // ... Load Other labels and give unique tags 

    } 

    UILabel *data1 = [cell viewWithTag:1]; 
    data1 setText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]]; 

    // ... Load other labels by tag and set text. 

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