Jaymin Raval
Jaymin Raval

Reputation: 205

How to Sort object by alphabet order for UITableViewCell?

I want to sort objects that i created & stored in NSMutableArray in AppDelegate.m. Stations is NSObject Class

I want to show station names in another UIViewController in alphabet order(in UITableViewCell) & when i click on them i want to pass the object that contains station name,latitude,longitude to next UIViewController

Currently i have extracted station name from stationList(Global NSMutableArray) to another NSMutableArray on UIViewControllers Cell & sorted it via

[sortedArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

but when didSelectRowAtIndexPath is being called i have to get this name from cell & search it in the stationList array to pass lat,long which is not good i think.

stationList Array Log(It has 100 objects):-

<__NSArrayM 0x79a2f110>(
<Stations: 0x78743540>,
<Stations: 0x78743630>,
<Stations: 0x78743670>,
<Stations: 0x78743750>,
<Stations: 0x78743830>,
<Stations: 0x78743910>,
<Stations: 0x78743a10>,
<Stations: 0x78743af0>
}

 -(void)loadStations
{
    stationList = [[NSMutableArray alloc]init];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"stations" ofType:@"txt"];
    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"%@",content);

    NSArray *tempArr = [content componentsSeparatedByString:@"\n"];

    for (int i =0; i<[tempArr count]; i++)
    {
        NSString *rawData = [tempArr objectAtIndex:i];

        if (rawData !=nil)
        {
            Stations *newStation = [[Stations alloc]init];

            NSArray *data = [rawData componentsSeparatedByString:@"\t"];

            newStation.sId = i+1;
            newStation.name = [NSString stringWithFormat:@"%@",[data objectAtIndex:0]];
            newStation.latitude = [[data objectAtIndex:1] doubleValue];
            newStation.longitude = [[data objectAtIndex:2] doubleValue];

            [stationList addObject:newStation];
        }
    }
}

Suggest me good practice/way for this, or maybe use Dictionary?

Upvotes: 0

Views: 1064

Answers (2)

Duncan C
Duncan C

Reputation: 131398

I would advise against sorting an array of station names separate from your array stationList. Instead I would suggest sorting your stationList (or a copy of it if you only want to change the oder in the table view and need to maintain some other ordering elsewhere)

There are methods like sortUsingComparator: that takes comparator block as a parameter. You write a block that compares 2 elements in your array, and the method uses that block to figure out the ordering of your objects and sort the array. In your case it would simply be a matter of writing a block that compares the name properties of 2 station objects.

Upvotes: 1

Jakub Vano
Jakub Vano

Reputation: 3873

I see two solutions here:

1) you can retrieve object from your stationList based on indexPath.row

- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    Stations* station = stationsList[indexPath.row];
    ...
}

2) you can create custom UITableViewCell and store referenced object there:

@interface StationCell : UITableVIewCell
    @property(weak) Stations* station;
@end

...

- (UITableViewCell*) tableView:(UITableVIew*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
     StationCell* cell;

     // dequeue StationCell
     ...

    cell.station = stationList[indexPath.row]; 
}

...


- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    StationCell* cell = [tableView cellAtIndexPath:indexPath];
    Stations* station = cell.station;
    ...
}

I would choose between solutions based on complexity of data displayed in cell - using custom UITableViewCell gives oportunity to move configuration of cell from view controller to cell implementation.

edit

As far as sorting stationsList, you can use e.g.:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
stationsList = [stationsList sortedArrayUsingDescriptors:@[sort]];

Upvotes: 1

Related Questions