简体   繁体   中英

sorting array of objects by property

I'm new to ios development and I'm trying to sort a tableview that gets populated by an array. I've looked into other solutions and for some reason the tableview isn't populating correctly. The array sorts in ascending order, but then the tableview doesn't display the objects in the correct order.

Here is what I have for the sorting:

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"_miles"
                                             ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [_addObjects sortedArrayUsingDescriptors:sortDescriptors];
_objects = [NSMutableArray arrayWithArray:sortedArray];

Here is my cellforrow :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%@", _objects);

    VenuesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    CustomObject *currentObject = [_objects objectAtIndex:indexPath.row];

    cell.venuesTitle.text = [currentObject nameOfVenue];
    NSString *preDistance = @"about";
    NSString *postDistance = @"miles";


    cell.venuesSubTitle.text = [NSString stringWithFormat:@"%@ %@ %@", preDistance,[_miles objectAtIndex:indexPath.row] ,postDistance];

    if (cell != nil) {
        NSMutableString *imagePath = [NSMutableString stringWithFormat:@"%@", [_paths objectAtIndex:indexPath.row]];
        [cell.venuesImage setContentMode:UIViewContentModeScaleAspectFit];
        [cell.venuesImage setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage imageNamed:@"placeholder@2x.png"]];

    }
    return cell;
}

when I log out the _objects array I get them in the right order, but they're not appearing in the right order in the tableview (the objects have a miles property that I'm trying to sort by).

You are sorting your array, _addObjects by miles and storing the result in _objects , but then, when you're displaying the results, you're not accessing miles from that sorted array you just created, but rather looking it up in some other array.

I don't know when you're populating that separate _miles array, but I'd suggest you retire it entirely and just make sure you get and set the miles property from the CustomObject instances. (Same for _paths ... this should probably be a property of your CustomObject , not a separate array.)

I don't see anything obvious, but a couple things that are a bit strange:

You should not use _objects . Always use self.objects instead. Most programming languages make it completely impossible to access pointers directly like you are doing here, and for good reason. It's dangerous and can lead to confusing bugs like this one. You should pretty much never access a property using _foobar . Always use self.foobar .

Why are you using NSMutableArray instead of NSArray ? Are you sure there isn't code somewhere else modifying it?

Where does VenuesTableViewCell *cell get created? I don't see anywhere in your code that one of these objects is created. [tableView dequeueReusableCellWithIdentifier..] never creates a new cell, it always returns an existing one or nil if none exists. Your code isn't throwing an assertion error (I assume?), so obviously it never returns nil . But there's something weird going on if that doesn't return nil the first time you call it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM