Question

            if(sort)
            {
                fql1 = [NSString stringWithFormat:@"SELECT uid,pic_square, name,online_presence FROM user WHERE is_app_user AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY last_name"];
            } else {
                fql1 = [NSString stringWithFormat:@"SELECT uid,pic_square, name,online_presence FROM user WHERE is_app_user AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY first_name"];
            }
            NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            fql1, @"q",
                                            nil];// fql query to fetch the user friend's

The UITableView is displaying the wrong order. How can I fix it to display the correct order?

--- CODE FOR UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProfileTableViewCell * cell = nil;
    NSString *cellid = @"ProfileTableViewCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellid];

    if (cell == nil) {
        cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

    Friends *friendsObj = [self.friendsArray objectAtIndex:indexPath.row];
    [cell setCellContentWithuser:friendsObj];

    return cell;
}

------------- Code to ProfileTableViewCell @interface ProfileTableViewCell : UITableViewCell {

    __weak IBOutlet UIImageView * imgvwUserPhoto;
    IBOutlet UILabel * labelForUserName;
    IBOutlet UIImageView * imgvwOnlinePresence;

}

- (void) setCellContentWithuser:(Friends*)friendsObj;

@end

My result is a tableview of friends that are always in the same sort order

Here is the list of my friends

Fa Hu
Ma Ku
Me Ma
Me Sh
Sy Ma

------ ProfileTableViewCell

-(NSArray *)getAllFriendsfromFriendsTable{

    NSFetchRequest *request = [self getBasicRequestForEntityName:@"Friends"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status=='1'"];
    NSSortDescriptor *catrgoryDescriptor = [[NSSortDescriptor alloc] initWithKey:@"facebook_name" ascending:YES];
    NSArray *sortDescriptors = @[catrgoryDescriptor];
    [request setSortDescriptors:sortDescriptors];
    [request setPredicate:predicate];
    NSArray *results = [_managedObjectContext executeFetchRequest:request error:nil];
    return results;
}

How do I sort this with my NSPredicate? The name is just 1 word. So I may need to split it and then sort by last name. (or first)

Was it helpful?

Solution

  1. Make sure the FQL is correct
  2. Make sure your results don't get handled by a dictionary or set - these don't respect order of the items (because of their data type) so you'll lose ordering there
  3. If it's still a problem, simply sort your array
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top