Note that there are some explanatory texts on larger screens.

plurals
  1. POSort NSArray with NSPredicate then store in a new array
    primarykey
    data
    text
    <p>I have a <code>UITableView</code> that gets populated via an array (<code>tableArray</code>), who gets populated from core data. </p> <p>each <code>UITableViewCell</code> gets assigned a number at creation time and the numbers are stored in an array. (<code>numberArray</code>) </p> <p>when the user reorders the rows, the numbers get moved around in the array (in conjunction with the <code>tableView</code> of course)</p> <p>So two Mutable arrays are used here. </p> <p>The <code>numberArray</code> holds the numbers (or the order) of the <code>TableViewCells</code>. I need to sort the array that holds the <code>UITableViewCell</code>'s text (<code>tableArray</code>) to reflect the same order that the <code>numberArray</code> holds. </p> <p>Also, this is important: as i said before, each cell gets assigned a number, this number is stored in the <code>numberArray</code>, </p> <p>I need both of the arrays to be sorted to hold the same values in the same place. </p> <p>So for example:</p> <p><code>tableArray</code> hold some objects:</p> <pre><code>1) hi 2) whats Up 3) this 4) is cool! </code></pre> <p>so as you can see each object here was assigned a number 1-4. and each of these numbers is added to the <code>numberArray</code>. </p> <p>The user can move the cells around so obviously the order of the numbers will change. </p> <p>So when the view loads up, i need to get the exact order of the <code>numberArray</code> whether it is </p> <pre><code>1,2,3,4 or 2,4,3,1 </code></pre> <p>and i need to sort the <code>tableArray</code> to reflect the same order as the <code>numberArray</code> so when the view loads up, if the <code>numberArray's</code> order is 2,3,4,1 i want the <code>tableArray's</code> order to be set to </p> <p>2"whats up", 3"this", 4"is cool!", 1"hi". </p> <p>I believe i can do this via <code>NSPredicate</code>. </p> <p>Any help is greatly appreciated!</p> <p><strong>EDIT</strong></p> <p>cellForRow:</p> <pre><code>-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * identifier = @"identifier"; self.myCell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (self.myCell == nil) { self.myCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } HandgunAmmo *handgunAmmo = [self.tableArray objectAtIndex:indexPath.row]; self.myCell.brandLabel.text = handgunAmmo.brand; self.myCell.caliberLabel.text = handgunAmmo.caliber; self.myCell.numberOfRoundsLabel.text = handgunAmmo.numberOfRounds; return self.myCell; } </code></pre> <p>And in my <code>viewWIllAppear</code> method:</p> <p>-(void)viewWillAppear:(BOOL)animated{</p> <pre><code>if (self.context == nil) { self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } NSFetchRequest *request = [[NSFetchRequest alloc]init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"HandgunAmmo" inManagedObjectContext:self.context]; [request setEntity:entity]; NSError *error; NSMutableArray *array = [[self.context executeFetchRequest:request error:&amp;error] mutableCopy]; [self setTableArray:array]; [self.ammoTable reloadData]; [super viewWillAppear:YES]; } </code></pre> <p>So, the reason why the array doesnt stay persistent when being changed is because im loading the data from core data, and i call <code>[self setTableArray:array];</code> which reloads all of the data from core data into the array, then it populates the tableview with the array. So i need to be able to sort the <code>array</code> before i set it equal to <code>self.tableArray</code>. </p> <p>Thank you for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload