I am making an app where i get data from an XML file, i parse it to an Array, and then it populates a TableViewController.
Now i want to find a way to save this display order, so whenever my app reloads the data it detects if the object has moved indexes or not and display and UP/DOWN/EQUAL sign accordingly.
E.g:
Loads data
1 = B
2 = A
3 = C
Reloads data
1 ^ C
2 = A
3 - B
I'm not sure how i can accomplish this, can anyone help me out?
Thank you.
There are multiple ways that you could do this, but the indexOfObject:
method is going to be your best bet for making it happen. With that method you could write something like:
NSArray *ar1 = @[@"first", @"second", @"third"];
NSLog(@"%d", (unsigned)[ar1 indexOfObject:@"third"]);
And it would log out 2
.
Based on that something like the following should work for you.
NSArray *ar1 = @[@"first", @"second", @"third"];
NSArray *ar2 = @[@"third", @"first", @"second"];
[ar2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx != [ar1 indexOfObject:obj]) {
NSLog(@"Obj: %@ Old idx: %ld new idx: %ld", obj, [ar1 indexOfObject:obj], idx);
}
}];
For me that outputs:
Obj: third Old idx: 2 new idx: 0
Obj: first Old idx: 0 new idx: 1
Obj: second Old idx: 1 new idx: 2
I think you need to keep a copy of the old data and then when the update arrives compare the data. Something like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSObject * obj = self.newData objectAtIndex:indexPath.row];
int oldIndex = [self.oldData indexOfObject:obj];
if (oldIndex == NSNotFound) { //new object
//do something with the new object
}
else
{
//do something with the oldIndex
}
}
Assuming oldData
contains the data fetched last time and newData
contains the just recived data
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.