In each custom cell, there is text in a uilabel that is loaded from a nsmutablearray.
One of the nsmutablearray's holds int's
The other nsmutablearray's holds nsdate in an unformatted form
I am trying to sort the cells based upon the highest int or the newest date. I got as far as getting the highest int in the array and getting the newest date but I am not sure how to reorder the cells in the way I explained. If anyone can help maybe make my current code better or help me redorder the rows that would be great. Also in the second if statement, how would I make a loop to loop through the array putting the highest int on top and it will ascend down? I am not asking for code but just some help/tips.
Here is my current code:
Code:
- (IBAction)sortbartouchdown {
TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
//Date
if(sortbar.selectedSegmentIndex == 0){
//Get Date and sort from newest
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE];
[appDelegate.Dates sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
//Now reorder tableview using the code above and animated
//How would I do this?
}
//Score
if(sortbar.selectedSegmentIndex == 1){
sortedScoreArray = [appDelegate.Score sortedArrayUsingSelector:@selector(intValue)];
//Move cells to appropriate places and animated
[sortedScoreArray lastObject];
}
}
Table view cells are meant to be transient views. Don't hang onto your cells and try to keep them sorted. Rather, just sort your data, and every time you're asked for a cell for some record, dequeue a reusable cell or create a new one, and then put the relevant data into it.
That is, don't return a certain cell for a certain index. Put certain data into a transient cell for a certain index.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
I am taking your advice and I am going to do it your way. I started to do this but there is a crash in my app, something like, something is not 'key-compliant' with nameLabel
This may not have to do with the sorting part but it is very similar to what I will want to do when I want to sort the cells so this will help.
I have a custom UITableViewCell class named TCell and I have 4 IBOutlets connecting UILablels and UIImageViews
I also have NSMutableArrays in my AppDelegate and if there is anything that is wrong in the following code just let me know!
It looks like you dont have something connected properly in IB, specifically your nameLabel. might wanna double check that it is connected.
edit
you are loading the TCell from your nib, and storing it in cell. Thats fine. You shouldnt be allocing a new TCell after that as you are doing. Use cell directlly and set the label values there.
Ok so I did as you said and the same problem. Everything is connected in the TCell class (Cell). This is the Statistics class if that makes a difference.
Here is the console error:
Code:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Statistics 0x15e910> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameLabel.'
*** Call stack at first throw:
Here is the cellforrowatindexpath method in Statistics
Well, it says the problem is with nameLabel, so I'd double and triple check that.
Also, all your code where you're setting the data properties of the cell should be moved outside your if block. Because you want that stuff to happen even if you successfully dequeue a reusable cell.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
I double checked and even tripled checked that everything is connected, I can even take a picture a post it if necessary. It is still the same problem but a little different now. Here is my new code:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Statistics 0x1ebbe0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key profilePicture.'
Can this crash happen if the Score, Dates, Names, or ProfilePictures array's have 0 inside?
<Statistics 0x1ebbe0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key profilePicture.
Somewhere, involving a Statistics object instance, you are attempting to do something involving profilePicture. Maybe you removed some code and still have a dangling connection in IB. So, find where you are doing that and remove it. If it is happening in the code you mention there, the problem is likely in the TCell class.
It is certainly not a dangling connection, I checked countless times. If it has to do with the TCell class, would you like me to post the code in it (not to much code)?
I just did and literally copied the code and changed the necessary variables to mine and still the same problem. Are there any specific problems you can think of?
My console still showed this during the crash:
Code:
'[<Statistics 0x1c9310> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key scoreLabel.
Ah yes! Ok so I adjusted the things that you tipped me on and now the console error has changed to something else for the crash.
It is now:
Code:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Ill recheck the connections but is there a reason for this so I am looking for something specific?