简体   繁体   中英

How to access to tableView cell in ios7

My cells contains from dynamic elements(2 separate core data entities connected with general user id). I need to sort it base on parameter of one element(date of sending message)(subtitle). How to access to constructed tableView cell?

===================

Now I sort cells base on my first entity, then base on its userId I add parameter from second entity. And I want to sort cell base on parameter of second entity

You can create an array of objects to use for the table. Then you can sort the array after.

First create a new object class to hold each item. This object will have properties for both of your entities.

Now go thru and fetch your entities and add each entity pair to an object.

Once you have your array of objects, you can do:

NSArray *sortedArray = [myObjectArray sortedArrayUsingComparator:^(MyObjectClass *a, MyObjectClass *b) {
    return [a.myParameter caseInsensitiveCompare:b.myParameter];
}];

Of course you would need to rename where necessary.

You should sort the array containing the core data entities and then update your tableView accordingly. Sort the array by adding an NSSortDescriptor to your NSFetchRequest.

you can use NSFetchedResultsController a sample code is here

Once you have an NSFetchedResultsController, it's just a matter of plugging the fetched objects into the table view data source methods, for example:

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

UITableViewCell *cell = <#Get the cell#>;
NSManagedObject *managedObject = [<#Fetched results controller#> objectAtIndexPath:indexPath];
// Configure the cell with data from the managed object.
return cell;
}

Hope this will help you out

It will help you in sortting cells on the basis of your second entity:
// Set example predicate and sort orderings...

    NSNumber *minimumSalary = ...;
   NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary];
[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

 NSError *error;
 NSArray *array = [moc executeFetchRequest:request error:&error];
 if (array == nil)
 {
// Deal with error...
  }

In addition to Amos's answer

NSSortDescriptor *sortDescriptor =  [[NSSortDescriptor alloc] initWithKey:@"lastMessageDate" ascending:NO];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:@[sortDescriptor]];

lastMessageDate - is a property of class with that I need sort

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