views:

75

answers:

1

I have a Station object defined as:

@interface RFStation : NSObject {
    NSString *stationID; 
    NSString *callsign; 
    NSString *logo; 
@end

I also have an NSMutableArray called 'StationList' which is a list of Station objects. This list is sorted alphabetically by 'callsign'.

I also have another NSArray called 'generalList', containing letters "A" through "Z"

I want to create a UITableView with section headers A-Z corresponding to the first letter of each callsign.

I currently have these methods & objects defined:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [generalList count];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [generalList objectAtIndex:section];
}

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

    NSPredicate *filter = [NSPredicate predicateWithFormat:@"callsign beginswith[cd] %@",[generalList objectAtIndex:section]];
    return [[stationList filteredArrayUsingPredicate:filter] count];
}

and of course:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RFStation *aStation = [stationList ObjectAtIndex:[indexPath row]];
    static NSString *identity = @"MainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];

    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 0.0) reuseIdentifier:identity] autorelease];
    }
    cell.text = aStation.callsign;

    return cell;
}

And yet my headers do not appear to sort the NSMutableArray data correctly. What is wrong with my code (I suspect it's the predicate)

A: 

You read [stationList ObjectAtIndex:[indexPath row]]. But the row of the NSIndexPath object is not overall it's the row in the section. You have to implement - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and return the exact number of rows per section. So you have to do the handling which cells are in which section on your own.

I would prefer a NSDictionary with a key for every letter in the alphabet and as their's object NSArray's with the Station objects.

V1ru8