2016-01-08 6 views
-1

Я динамически создал UITableView и пытался добавить UILabel и UIButton, но когда мы откроем UIViewController, все будет правильно, и когда я попытаюсь прокрутить все, все перепуталось.Содержимое UITableView сливается при прокрутке?

Динамическое добавление UITextFields и UIButtons на основе идентификатора поля, который я получаю с сервера.

вот мой cellForRowAtIndexPath метод

Обновления

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier; 
    if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){ 
     MyIdentifier = @"CharCell"; 

    }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){ 
     MyIdentifier = @"IntCell"; 
    }else{ 
     MyIdentifier = @"IntCell11"; 
    } 
    cell.textLabel.text = Nil; 

    cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:MyIdentifier]; 

     } 
     cell.selectionStyle=UITableViewCellSelectionStyleNone; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.accessoryType = UITableViewCellAccessoryNone; 

     //cell.clipsToBounds=YES; 
     if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){ 
      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 

      [cell.contentView addSubview:[self getVarcharTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){ 
      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 

      [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"SINGLESELECT"]){ 
      NSString *title=[[selectedTabFields valueForKey:@"name"]objectAtIndex:indexPath.row]; 

      NSLog(@"single runing"); 

      [cell.contentView addSubview:[self getSingleSelectLabel:indexPath.item :title]]; 

      [cell.contentView addSubview:[self addView:indexPath.row :[dynamicOptionFields objectAtIndex:indexPath.row] :[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"MULTISELECT"]){ 

      NSLog(@"runing"); 
      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 

      [cell.contentView addSubview:[self addMultiSelectView:indexPath.row:[dynamicOptionFields objectAtIndex:indexPath.row]:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     } 




    return cell; 

} 

Пожалуйста, проверьте Прилагаемый скриншоты

при загрузке страницы enter image description here

Когда я прокрутите вниз она станет так enter image description here

Теперь он не сливается, но когда я прокручиваю вниз, я вижу повторяющиеся ячейки. Для например: я могу видеть мои первые строки несколько раз (в конце свитка)

последние скриншоты enter image description here

+0

если ([[[selectedTabFields valueForKey: @ "тип"] objectAtIndex: indexPath.row] isEqualToString: @ "INTEGER"]) { MyIdentifier = @ "IntCell"; } else { MyIdentifier = @ "IntCell"; } после проверки этих операторов буксировки оба имеют одинаковые идентификаторы, которые изменяют их однозначно – Smile

+0

Грязный способ решить это будет 'cell = [tableView1 dequeueReusableCellWithIdentifier: nil];' Но я снова говорю, что это грязный путь. – rptwsthi

+0

позвольте мне попробовать свои ответы – Bangalore

ответ

1

Как вы добавляете тот же вид снова и снова на tableviewcell, без проверки, если она уже или нет. Вместо этого вы можете добавить один раз, а затем играть со скрытым/скрытым контентом в зависимости от вашего состояния.

Существует еще один способ: вы можете установить тег для каждого из подвью и проверить, если [cell viewWithTag: x] (x: Целое число), доступно, затем удалите его из представления или используйте тот же вид.

//cell.clipsToBounds=YES; 
     if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){ 
// make check here before adding 
      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 
// make check here before adding 
      [cell.contentView addSubview:[self getVarcharTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){ 
// make check here before adding    
[cell.contentView addSubview:[self getLabel:indexPath.row]]; 
// make check here before adding 
      [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     } 
+0

Пожалуйста, уточните мой обновленный код – Bangalore

0

пожалуйста Измените код, как показано ниже

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier; 
    if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){ 
     MyIdentifier = @"CharCell"; 

    }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){ 
     MyIdentifier = @"IntCell"; 
    }else{ 
     MyIdentifier = @"IntCell11"; 
    } 


    cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:MyIdentifier]; 
     } 
    cell.textLabel.text = Nil; 
      cell.selectionStyle=UITableViewCellSelectionStyleNone; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.accessoryType = UITableViewCellAccessoryNone; 

     //cell.clipsToBounds=YES; 
     if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){ 
      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 

      [cell.contentView addSubview:[self getVarcharTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){ 
      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 

      [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"SINGLESELECT"]){ 
      NSString *title=[[selectedTabFields valueForKey:@"name"]objectAtIndex:indexPath.row]; 

      NSLog(@"single runing"); 

      [cell.contentView addSubview:[self getSingleSelectLabel:indexPath.item :title]]; 

      [cell.contentView addSubview:[self addView:indexPath.row :[dynamicOptionFields objectAtIndex:indexPath.row] :[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

     }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"MULTISELECT"]){ 

      [cell.contentView addSubview:[self getLabel:indexPath.row]]; 

      [cell.contentView addSubview:[self addMultiSelectView:indexPath.row:[dynamicOptionFields objectAtIndex:indexPath.row]:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]]; 

    return cell; 

} 
+0

, но как он меняет порядок и как он сменяет друг друга? – Bangalore

+0

его не работает – Bangalore

+0

@ Бангалор проверить подключение dynamicCell и init – Smile

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