2012-02-24 22 views
12

Ho appena iniziato a programmare per iPhone e sto creando un'applicazione che si collega a un database e ottiene un set di nomi di riga e li visualizza. Se selezionato, il colore di sfondo delle righe cambia, ovvero puoi effettuare selezioni multiple e saranno tutti colori diversi. Quindi sto recuperando l'XML dal server senza problemi e ho creato un UITableView per visualizzare le celle. Tuttavia, non ho idea di come aggiungere le celle alla tabella. Ho dato un'occhiata a insertRowsAtIndexPaths ma non sono sicuro di come usarlo? Come ho capito, insertRowsAtIndexPaths accetta due parametri:Aggiunta di celle a UITableView

Un NSArray che contiene la riga in cui si suppone che si trovi la cella e in quale sezione. Il problema con questo è che la mia app avrà un numero dinamico di righe. Come farei a creare il NSArray se non so quante righe avrò? Posso usare NSMutableArray?

Il secondo parametro che serve è un'animazione, che è piuttosto semplice.

Un altro problema che sto avendo è dove creo effettivamente le celle? Come passi le celle alla tableview?

Ho provato a leggere la documentazione ma non sembra molto chiaro! Ecco il codice che ho in questo momento all'interno del metodo loadview del controller della vista:

//Before this I get the XML from the server so I am ready to populate 
//cells and add them to the table view 
NSArray *cells = [NSArray arrayWithObjects: 
        [NSIndexPath indexPathForRow:0 inSection:0], 
        [NSIndexPath indexPathForRow:1 inSection:0], 
        [NSIndexPath indexPathForRow:2 inSection:0], 
        [NSIndexPath indexPathForRow:3 inSection:0], 
        [NSIndexPath indexPathForRow:4 inSection:0], 
        [NSIndexPath indexPathForRow:5 inSection:0], 
        [NSIndexPath indexPathForRow:6 inSection:0], 
        [NSIndexPath indexPathForRow:7 inSection:0], 
        [NSIndexPath indexPathForRow:8 inSection:0], 
        [NSIndexPath indexPathForRow:9 inSection:0], 
        [NSIndexPath indexPathForRow:10 inSection:0], 
        [NSIndexPath indexPathForRow:11 inSection:0], 
        [NSIndexPath indexPathForRow:12 inSection:0], 
        nil]; 
[eventTypesTable beginUpdates]; 
[eventTypesTable insertRowsAtIndexPaths:cells withRowAnimation:UITableViewRowAnimationNone]; 
[eventTypesTable endUpdates]; 

risposta

18

Penso che ci si avvicina questo dalla direzione sbagliata. UITableViews non funziona come te lo aspetti. insertRowsAtIndexPaths è per l'inserimento di nuove righe in una tabella, piuttosto che per popolarlo in prima istanza.

UITableViews funziona chiamando un numero di metodi delegati che consentono di presentare i dati alla visualizzazione tabella come necessario. Il framework si occupa quindi del sollevamento pesante per popolare le celle, gestire gli eventi di scorrimento e tocco, ecc.

Si consiglia di iniziare leggendo un tutorial come questo: http://www.iosdevnotes.com/2011/10/uitableview-tutorial/, che per me è abbastanza completo. Spiega come impostare l'origine dati per la tabella e come è possibile configurare il modo in cui i dati vengono presentati da UITableView.

Buona fortuna!

+0

Grazie, vedo ero completamente confuso su come che ha funzionato. Sono riuscito a generare correttamente le celle ora, ma sto riscontrando un problema. Sto recuperando 12 righe dal database, tuttavia, lo schermo si adatta solo a 7. Una volta che lo schermo è pieno e nel simulatore, se provo a scorrere verso il basso, ottengo un errore in 'NSString * sEventType = [[eventTypes valueForKeyPath: @" nome .text "] objectAtIndex: indexPath.row];' all'interno del metodo 'cellForRowAtIndexPath'. C'è qualcosa che mi manca? Come devo aggiungere un controller scroll o qualcosa del genere? Ancora una volta, grazie per la pronta e utile risposta! – KerrM

+7

Il collegamento è interrotto. Questo è il motivo per cui è necessario inserire almeno un codice di esempio anziché fare affidamento su collegamenti esterni. –

+0

collegamento è rotto, ma è possibile trovare il contenuto di questo URL qui: http://web.archive.org/web/20150928131750/http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ – Upsilon42

16

Non è necessario utilizzare insertRowsAtIndexPaths.

Check: UITableViewDataSource Protocol Reference e UITableView Class Reference

la magia accade tra questi tre metodi (metodi protocollo UITableViewDataSource):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

Hai solo bisogno di riempire un array. Sì, può essere un NSMutableArray.

È possibile compilare la matrice in - (void)viewDidLoad, ad esempio:

yourItemsArray = [[NSMutableArray alloc] initWithObjects:@"item 01", @"item 02", @"item 03", nil]; 

E li utilizzare i metodi di origine dati come questo:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    // If You have only one(1) section, return 1, otherwise you must handle sections 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [yourItemsArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

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

    // Configure the cell... 
    cell.textLabel.text = [NSString stringWithFormat:[yourItemsArray objectAtIndex:indexPath.row]]; 

    return cell; 
} 

come questo le cellule verranno creati automaticamente.

Se chage l'Array, solo bisogno di chiamare:

[self.tableView reloadData]; 
+0

Grazie per la tua risposta, vorrei poter selezionare più di una risposta come accettata. – KerrM

+3

Va bene, ci sono diversi approcci allo stesso problema. E se stai cominciando a imparare, dovresti prendere i tutorial per capire come funzionano le cose, non solo il codice. Ma entrambi aiutano veramente – Frade

+0

Sì, immagino che ci sarebbe. Ho incontrato un altro problema, spero che tu possa aiutarti - sto recuperando 12 righe dal database, tuttavia, lo schermo si adatta solo a 7. Una volta che lo schermo è pieno e nel simulatore, se provo a scorrere verso il basso, ottengo un errore in NSString * sEventType = [[eventTypes valueForKeyPath: @ "nome.text"] objectAtIndex: indexPath.row]; all'interno del metodo cellForRowAtIndexPath. È quasi come se il metodo smettesse di funzionare una volta arrivato nella parte inferiore dello schermo. È qualcosa che ho fatto di sbagliato? – KerrM

2
//######## Adding new section programmatically to UITableView ############ 

    @interface MyViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
    { 
     IBOutlet UITableView *tblView; 
     int noOfSection; 
    } 
    -(IBAction)switchStateChanged:(id)sender; 
    @end 



    @implementation MyViewController 
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ 
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
     if (self) { 
      // Custom initialization 
     } 
     return self; 
    } 
    - (void)viewDidLoad{ 
     [super viewDidLoad]; 

     noOfSection = 2; 
    } 
    - (void)viewDidUnload{ 
     [super viewDidUnload]; 
    } 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 

      return YES; 
     } 

     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } 
    #pragma mark - TableView Delegate Methods 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
     return noOfSection; 
    } 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

     return 1; 
    } 
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     if(indexPath.section == 2){ 
      return 200; 
     } 
     return 50; 
    } 

    - (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]; 

      UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 20, 10)]; 
      cell.accessoryView = switchBtn; 

      [switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged]; 
      cell.textLabel.font = [UIFont systemFontOfSize:14]; 
      cell.detailTextLabel.font = [UIFont systemFontOfSize:11]; 
      cell.textLabel.numberOfLines = 2; 
      cell.detailTextLabel.numberOfLines = 2; 
     } 



     if(indexPath.section == 0){ 
      cell.textLabel.text = @"Cell-1 Text"; 
      cell.detailTextLabel.text = @"Cell-1 Detail text"; 
     } 
     else if(indexPath.section == 1){ 
      cell.textLabel.text = @"Cell-2 Text"; 
     } 
     else { // new added section code is here... 
      cell.textLabel.text = @"New Added section"; 
     } 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     return cell; 
    } 
    -(IBAction)switchStateChanged:(id)sender{ 
     UISwitch *switchState = sender; 

     if(switchState.isOn == YES){ 
      NSLog(@"ON"); 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2]; 
      [self insertNewSectionWithIndexPath:indexPath]; 
     } 
     else { 
      NSLog(@"OFF"); 
      [self removeSectionWithIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]]; 
     } 
    } 
    -(void)insertNewSectionWithIndexPath:(NSIndexPath *)indexPath{ 


     noOfSection = 3; 
     [tblView insertSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    -(void)removeSectionWithIndexPath:(NSIndexPath *)indexPath{ 
     noOfSection = 2; 
     [tblView deleteSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    @end 
Problemi correlati