2009-11-02 5 views
0

J'ai une vue de table indexée qui organise les villes dans un état par leur première lettre. Ce que je travaille est un NSMutableDictionary est créé avec les clés de A, B, C, etc. et les villes correspondantes sont ajoutées à leurs tableaux respectifs. ex:UITableView Indexation

Y =  (
    Yatesboro, 
    Yeagertown, 
    York, 
    "York Haven", 
    "York New Salem", 
    "York Springs", 
    Youngstown, 
    Youngsville, 
    Youngwood, 
    Yukon 
); 
Z =  (
    Zelienople, 
    Zieglerville, 
    "Zion Grove", 
    Zionhill, 
    Zionsville, 
    Zullinger 
); 

maintenant, mon point de vue de la table des charges avec le nombre correct de sections et les lignes dans les sections et le contrôle d'indexation fonctionne très bien avec ceci:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [cities count]; 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if([searchArray count]==0) 
     return @""; 
    return [searchArray objectAtIndex:section]; 
} 
// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[cities objectForKey:[searchArray objectAtIndex:section]] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 
    NSLog([NSString stringWithFormat:@"clicked index : %i",index]); 
    if (index == 0) { 
     [tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO]; 
     return -1; 
    } 
    return index; 
} 

Mon problème peuplait maintenant le texte de la cellule de table avec le texte pour chaque section ... Des pensées sur comment je peux saisir cette information?

Répondre

1

le cellForRowAtIndexPath reçoit un paramètre de type NSIndexPath transmis qui contient à la fois la ligne et la section requises. Voir NSIndexPath UIKit Additions pour plus de détails.

Cela étant dit, cela pourrait fonctionner dans votre cas particulier:

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

    NSString *letter = [searchArray objectAtIndex:indexPath.section]; 
    NSArray *city = [cities objectForKey:letter]; 
    cell.text = [city objectAtIndex:indexPath.row]; 

    return cell; 
} 

Je ne sais pas si je me trompe pas (n'ont pas essayé un compilateur), mais vous pouvez obtenir maintenant l'idée générale :)

+0

A travaillé comme un homme de charme. Sauvé un mal de tête majeur pour moi. Merci! – rson