2013-05-25 11 views
10

Ho quattro UITableViews side-by-side, creato a livello di programmazione. Problema 1: la prima cella è sempre vuota. Problema 2: quando scorro verso il basso, alcune celle sono vuote. Quando faccio scorrere avanti e indietro, altre cellule scompaiono.Alcune celle UITableView scompaiono dopo lo scorrimento verso il basso e quindi indietro

Ecco il codice in cui vengono create le UITableViews, ed è ubicato loro cellule ...

Array of UITableViews dichiarato all'inizio del file di .m:

UITableView* channel_tableView[ TOTAL_TX_CHANNELS ]; 

In funzione viewDidLayoutSubviews ho creare il UITableView :

for(int channel=0; channel < TOTAL_TX_CHANNELS; ++channel){ 
CGRect tableFrame = CGRectMake(x, y, width, height); 

channel_tableView[ channel ] = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain]; 

channel_tableView[ channel ].scrollEnabled = YES; 
channel_tableView[ channel ].userInteractionEnabled = YES; 
channel_tableView[ channel ].bounces = YES; 

channel_tableView[ channel ].delegate = self; 
channel_tableView[ channel ].dataSource = self; 

channel_tableView[ channel ].autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
[channel_tableView[ channel ] reloadData];    // display channel's TableView 
[[self view] addSubview: channel_tableView[ channel ]]; 

}

012.351.641.061.

qui è dove ogni cella è IMPOSTATO .......................

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"cellForRowAtIndexPath"); 
    NSLog(@" %d = row", indexPath.row ); 



    UITextView* test_textView; 
    UITextView* test_2nd_textView; 
    UITextView* test_3rd_textView; 


    // Determine which channel_tableView: 
     int channel; 
     for(channel = 0; channel < TOTAL_TX_CHANNELS; ++channel) 
     { 
      if(tableView == channel_tableView[ channel ]) 
       break; 
     } 
     // channel = tableView's channel 
     NSLog(@" %d = channel", channel ); 

    // DOCUMENTATION: Table View Programming Guide for iOS > Adding subviews to a cellís content view 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     NSLog(@"  cell nil"); 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; 
     /* Though it's UITableViewCellStyleDefault, the three defaults (image, label, detail label) are nil 
     if not set. */ 
    } 
     // Add UITextView for channel pipe to cell: 
      int pipe_width = 20; 
      int w = pipe_width; 
      int x = channel_tableView_width/2 - pipe_width/2; 
      int h = channel_row_height; 
      int y = 0; 
      test_textView = [[UITextView alloc] initWithFrame:CGRectMake (x,y, w,h)] ; 
      [test_textView setFont:[UIFont boldSystemFontOfSize:8.0]]; 
      [test_textView setEditable:NO]; 
      [test_textView setTextAlignment:NSTextAlignmentCenter]; 
      // Round the corners and set border color 
      if(channel == power_channel) 
       [test_textView setBackgroundColor:[UIColor greenColor]]; 
      else 
       [test_textView setBackgroundColor:[UIColor whiteColor]]; 
      [[test_textView layer] setBorderColor:[[UIColor blackColor] CGColor]]; 
      [[test_textView layer] setBorderWidth:1]; 
      //[[test_textView layer] setCornerRadius:15]; 
      [test_textView setClipsToBounds: YES]; 


     // Add UITextView for PWR RX to cell: 
     int PWR_RX_width = channel_tableView_width/2; 
     y = y + h ; 
     w = PWR_RX_width; 
     x = channel_tableView_width/2 - PWR_RX_width/2; 
     h = 20; 
      test_2nd_textView = [[UITextView alloc] initWithFrame:CGRectMake (x,y, w,h)]; 
      [test_2nd_textView setFont:[UIFont boldSystemFontOfSize:8.0]]; 
      [test_2nd_textView setEditable:NO]; 
      [test_2nd_textView setTextAlignment:NSTextAlignmentCenter]; 
      // Round the corners and set border color 
      [test_2nd_textView setBackgroundColor:[UIColor whiteColor]]; 
      [[test_2nd_textView layer] setBorderColor:[[UIColor blackColor] CGColor]]; 
      [[test_2nd_textView layer] setBorderWidth: 1]; 
      //[[test_2nd_textView layer] setCornerRadius:15]; 
      [test_2nd_textView setClipsToBounds: YES];  

     // Add UITextView for device to cell: 
     int device_width = channel_tableView_width/2; 
     y = y + h-3; 
     w = device_width; 
     x = channel_tableView_width/2 - device_width/2; 
     h = 40; 
      test_3rd_textView = [[UITextView alloc] initWithFrame:CGRectMake (x,y, w,h)]; 
      [test_3rd_textView setFont:[UIFont boldSystemFontOfSize:8.0]]; 
      [test_3rd_textView setEditable:NO]; 
      [test_3rd_textView setTextAlignment:NSTextAlignmentCenter]; 
      // Round the corners and set border color 
      [test_3rd_textView setBackgroundColor:[UIColor whiteColor]]; 
      [[test_3rd_textView layer] setBorderColor:[[UIColor blackColor] CGColor]]; 
      [[test_3rd_textView layer] setBorderWidth: 1]; 
      //[[test_3rd_textView layer] setCornerRadius:15]; 
      [test_3rd_textView setClipsToBounds: YES]; 

     /* 
       test_textView.tag = TEST_TEXTVIEW_TAG; 
       test_2nd_textView.tag = TEST_2ND_TEXTVIEW_TAG; 
       test_3rd_textView.tag = TEST_3RD_TEXTVIEW_TAG; 

     test_textView  = (UITextView*)[cell.contentView viewWithTag: TEST_TEXTVIEW_TAG]; 
     test_2nd_textView = (UITextView*)[cell.contentView viewWithTag: TEST_2ND_TEXTVIEW_TAG]; 
     test_3rd_textView = (UITextView*)[cell.contentView viewWithTag: TEST_3RD_TEXTVIEW_TAG]; 
     */ 
    //[test_textView setText:  [NSString stringWithFormat: @"pipe-%d", indexPath.row ]]; 
    [test_2nd_textView setText: [NSString stringWithFormat: @"PWR RX %d", indexPath.row ]]; 
    [test_3rd_textView setText: [NSString stringWithFormat: @"device %d", indexPath.row ]]; 
      // Add created UI objects to cell[ indexPath.row ]: 
       [cell.contentView addSubview:test_textView ]; 
       [cell.contentView addSubview:test_2nd_textView ]; 
       [cell.contentView addSubview:test_3rd_textView ]; 
    return cell; 
} 
+0

non si dovrebbe usare lo stesso cellIdentifier per tutte le tabelle. prova [NSString stringWithFormat: @ "cell% d", chanel]; – Durgaprasad

risposta

14

è necessario definire il CellIdentifier come Bellow: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row]; 



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = @"Test"; 
    cell.textLabel.textColor=[UIColor whiteColor]; 
    return cell; 
} 

NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];

questo può aiutare

+1

Grazie, Nitin! Questo mi ha portato a capire finalmente il comportamento della struttura dei dati delle celle. Nel mio caso, ho avuto 4 tableViews, quindi il mio identificatore di cella deve essere una funzione del numero di canale e del numero di riga. Ora scorre senza modificare i valori. O N E L A S T B U G: La prima riga all'inizio di ciascuna delle 4 tabelle è sempre vuota, anche dopo averlo fatto scorrere verso il basso e quindi indietro. Ma NSLog mostra che la riga 0 ottiene una cella. –

+0

Ciao dauglas fai riferimento a questo: -http: //stackoverflow.com/questions/6805669/ios-objective-c-only-the-first-cell-of-uitableview-doesnt-show-the-label-i-add –

+1

+1 Grazie mille, questo mi ha aiutato – iMeMyself

0

u deve affiancare una serie di dizionario e aggiungere il tuo testo filtra i dati che sono basati su indexpath.row dopo scroll tableview.

vedi il mio esempio

NSMutableDictionary *dicttable=[NSMutableDictionary dictionaryWithDictionary:[arrofselect  objectAtIndex:indexPath.row]]; 
    [dicttable setValue:[NSString stringWithFormat:@"%@",self.other_text.text] forKey:@"AnswerId"]; 
    [dicttable setValue:[[arrQuestions objectAtIndex:indexPath.row] valueForKey:@"QuestionID"] forKey:@"QuestionID"]; 
    [arrofselect replaceObjectAtIndex:indexPath.row withObject:dicttable]; 

u hanno sostituire quella indexapth.row valori delle variabili del dizionario based quando la modifica del testo in

  • (void) textFieldDidBeginEditing: (UITextField *) textField
  • (void) textFieldDidEndEditing: (UITextField *) textField

enjoy ... chee rs

+0

se hai domande fammi sapere ... – Anjaneyulu

4

Ho avuto lo stesso problema ma per me erano le proprietà degli elementi all'interno di UITableviewCell. Suggerisco di utilizzare una cella personalizzata e creare i campi di testo con NIB o StoryBoard. Quindi creare sbocchi per loro nella classe cella personalizzato con la seguente firma:

@property (strong, nonatomic) IBOutlet UITextField *myText; 

La parte importante è utilizzare il forte e non la proprietà deboli.

Questo è un link per il mio post.

Buona fortuna e spero che questo aiuto!

+0

Non sono riuscito a utilizzare lo storyboard perché i bambini degli oggetti dell'interfaccia utente nelle celle cambieranno durante l'esecuzione.Ma suppongo che potrei popolare ogni cella in fase di compilazione con lo stesso identico insieme di oggetti dell'interfaccia utente, e quindi nascondere/mostrare ciò che è necessario in fase di esecuzione. –

Problemi correlati