2013-10-22 16 views
6

Ho un controller TableView che desidero popolare con gli oggetti da un array. Sto usando StoryBoard. Inoltre, non sono sicuro di dover inserire etichette nel Prototipo cella nello storyboard come tipo di segnaposto?Come popolare un TableView con una matrice di oggetti?

My aList La matrice mobile contiene oggetti di tipo Homework. In ogni riga della tabella, voglio visualizzare (Queste variabili sono già impostati nel mio Homework Modello):

-ClassName

-AssignmentTitle

-DueDate

Ecco quello che ho attualmente hanno

TableViewController.h

@interface AssignmentTableController : UITableViewController 
< 
AssignmentDelegate 
> 

@property(strong,nonatomic) Assignment *vc; 
@property (strong, nonatomic) NSMutableArray *alist;//array was filled with delegation 


@end 

TableViewController.m

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

// Return the number of rows in the section. 
return [self.alist count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier  forIndexPath:indexPath]; 
Homework *ai = [self.alist objectAtIndex:indexPath.row]; 

//*******I am not sure what to do from Here****** 
//*******I need to start displaying 1 object per row,displaying what was stated above*** 

    // Configure the cell... 

return cell; 
} 
+1

possibile duplicato di [Popolare un UITableView da array] (http://stackoverflow.com/questions/4367393/populating-a-uitableview-from-arrays) –

risposta

7

ho un'implementazione simile.

Implementazione: Ho creato i seguenti metodi. (Li ho modificato per adattare il codice fornito.)

-(Homework*) homeworkAtIndex: (NSInteger) index 
{ 
    return [alist objectAtIndex:index] ; 
} 

-(NSInteger) numberOfObjectsInList 
{ 
    return [alist count]; 
} 

E nel metodo delegato UITableViewController:

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section 

ho usato questo metodo di chiamata

return [self numberOfObjectsInList]; 

Nel metodo delegato:

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SubtitleIdentifier] autorelease]; 
    Homework *ai = [self homeworkAtIndex: indexPath.row]; 

    /* your other cell configurations 
    cell.textLabel.text = ai.className; // eg. display name of text 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ | %@",ai.assignmentTitle,ai.dueDate]; // will show the cell with a detail text of "assignment title | due date" eg. "Lab 1 | 23 Oct 2013" appearing under the className called "Physics" 
    */ 
} 

Utilizzo dei compiti del metodoAtIndex consente di popolare i diversi oggetti dell'array nelle diverse celle della tabella.

In questo modo ho dovuto creare celle personalizzate e formattare le dimensioni della cella per adattarle al tavolo. Forse questo può funzionare per te se i dati che verranno mostrati non sono così lunghi, e in realtà non devono usare le celle personalizzate.(Un po 'come l'esempio che ho fornito)

Se vi stavate chiedendo come controllare per le diverse celle selezionate (come si potrebbe desiderare di spingerli ad una viewController diverso da allora in poi), si può fare questo nel metodo delegato:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    UIViewController* nextViewController = nil; 
    NSString* cellDisplayName = [delegate homeworkAtIndex: indexPath.row].name; // can be the Homework object if you want 
    if([cellDisplayName isEqualToString:[self homeworkAtIndex:0].className]) 
     nextViewController = [[firstViewController alloc] init]; 
    else if([cellDisplayName isEqualToString:[self homeworkAtIndex:1].className]) 
     nextViewController = [[secondViewController alloc] init]; 
    // goes on for the check depending on the number of items in the array 
} 

Sto usando NSString qui per verificare la classeName poiché non sono sicuro di quale sia l'oggetto Homework, ma puoi sicuramente cambiare la logica per adattarla all'oggetto Homework, poiché suppongo che potresti avere oggetti diversi con stessa variabile className.

Spero che questo aiuti! :)

Problemi correlati