2011-08-06 8 views
0

J'ai un tableau avec le format suivant:Comment structurer un tableau en UITableView?

[NSArray arrayWithObjects: 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
nil]; 

Je veux structurer ces données à charger dans mon tableView.

Chaque ligne de la tableview doit correspondre à chaque ligne du tableau, le titre de chaque cellule doit correspondre au sous-objet objectAtIndex 2 pour le nom.

+0

Il me semble que ce code est déjà 'structuré' pour un 'UITableView'. Cherchez-vous le code pour implémenter la création de 'UITableViewCell's? – GarlicFries

+0

il semble que vous faites cela dans le mauvais sens ... vous avez besoin d'être plus précis sur la façon dont vous voulez que la table apparaisse –

+0

Oui, je suppose, en cherchant à installer les cellules. Quelque chose comme cell.textlabel.text = [objet de tableau à quoi?]] Parce que c'est un tableau dans un tableau. – Jon

Répondre

2

Supposons que votre tableau est nommé myData:

- (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 [myData 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... 
    NSArray *obj = [myData objectAtIndex:indexPath.row]; 
    cell.textLabel.text = (NSString*)[obj objectAtIndex:1]; //note: 0=>number; 1=>name,.. 

    return cell; 
} 

En faveur de réutilisabilité, je suggère de remplacer les sous-tableaux avec NSDictionaries de sorte que vous pouvez obtenir par exemple le nom en appelant [dict objectForKey:@"name"].

Questions connexes