Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-01-2011, 03:42 PM   #1 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Question Sorting Custom Cells by NSArray?

Hey,

I am loading my tableview like this:
Code:
TCell *cell = (TCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TCell" owner:self options:nil];
		cell = (TCell *)[array objectAtIndex:0];
	}
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];
		
	}
	
	
}
Thanks!
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-01-2011, 06:42 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

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.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 06-01-2011, 11:14 PM   #3 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Thanks,

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!

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{	
	static NSString *CellIdentifier = @"CellIdentifier";
	
	TCell *cell = (TCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TCell" owner:self options:nil];
		cell = (TCell *)[array objectAtIndex:0];
		
		//Put the score, date, name, and profilePicture into the appropriate cell row
		TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
		TCell *tc = [[TCell alloc] init];
		tc.scoreLabel = [appDelegate.Score objectAtIndex:indexPath.row];
		tc.dateLabel = [appDelegate.Dates objectAtIndex:indexPath.row];
		tc.nameLabel = [appDelegate.Names objectAtIndex:indexPath.row];
		tc.profile.image = [appDelegate.ProfilePictures objectAtIndex:indexPath.row];
		[tc release];
	}
	return cell;
}
Any reason why that would cause a crash like I mentioned above? Can it be because of the arrays being 0?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-02-2011, 09:38 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

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.
smithdale87 is offline   Reply With Quote
Old 06-02-2011, 09:37 PM   #5 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSLog(@"cellForRowAtIndexPath");
	
	static NSString *CellIdentifier = @"CellIdentifier";
	
	TCell *cell = (TCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TCell" owner:self options:nil];
		cell = (TCell *)[array objectAtIndex:0];
		
		//Put the score, date, name, and profilePicture into the appropriate cell row
		TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
		cell.scoreLabel = [appDelegate.Score objectAtIndex:indexPath.row];
		cell.dateLabel = [appDelegate.Dates objectAtIndex:indexPath.row];
		cell.nameLabel = [appDelegate.Names objectAtIndex:indexPath.row];
		cell.profilePicture.image = [appDelegate.ProfilePictures objectAtIndex:indexPath.row];
	}
	
	return cell;
	
}
Any ideas?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-02-2011, 10:39 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

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.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 06-03-2011, 04:26 PM   #7 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSLog(@"cellForRowAtIndexPath");
	
	static NSString *CellIdentifier = @"CellIdentifier";
	
	TCell *cell = (TCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TCell" owner:self options:nil];
		cell = (TCell *)[array objectAtIndex:0];
	}
	
	//Put the score, date, name, and profilePicture into the appropriate cell row
	TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
	cell.scoreLabel = [appDelegate.Score objectAtIndex:indexPath.row];
	cell.dateLabel = [appDelegate.Dates objectAtIndex:indexPath.row];
	cell.nameLabel = [appDelegate.Names objectAtIndex:indexPath.row];
	cell.profilePicture.image = [appDelegate.ProfilePictures objectAtIndex:indexPath.row];
	
	return cell;
	
}
Console Crash Log:
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?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-04-2011, 02:00 PM   #8 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Bump. Any Ideas?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-04-2011, 02:30 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
<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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-05-2011, 08:33 PM   #10 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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)?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-05-2011, 09:28 PM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

In TCell.xib, what class is File's Owner?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-05-2011, 09:36 PM   #12 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

UITableViewCell
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-05-2011, 09:39 PM   #13 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You're doing something wrong then. Go to the table view link in my signature.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-05-2011, 10:10 PM   #14 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-05-2011, 10:13 PM   #15 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Yes. Broken connection. Wrong class.

And the stuff to take away from the tutorial is not copy-pasteable. It is in the xib.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-05-2011, 10:19 PM   #16 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-05-2011, 10:21 PM   #17 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Ahhhhhh! I just forgot one last connection so now it works! Thank you!
btw have fun at WWDC :P
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 06-05-2011, 10:22 PM   #18 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You didn't make the cell connection.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
array

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 332
4 members and 328 guests
Dnnake, iOS.Lover, jenniead38, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0