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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 12-20-2008, 03:44 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 180
Default Problem with uitableview and editing, keeps crashing!

Ok, well I have my uitableview set up and im having a problem with it when im editing it. It just keeps crashing when I try to delete an item in my table. I checked the debugger and there is no explination as to why its crashing, please help me. I know the error is in this function somewhere, thanks!:

Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (editingStyle == UITableViewCellEditingStyleDelete) 
	{
		NSMutableDictionary *startDict2 = [NSMutableDictionary dictionaryWithDictionary:[[[NSUserDefaults standardUserDefaults] objectForKey:nameString] mutableCopy]];
		NSMutableDictionary *nextDict2 = [NSMutableDictionary dictionaryWithDictionary:[[startDict2 objectForKey:@"Assignments"] mutableCopy]];
		
		if ([nextDict2 count] > 0) {
			NSArray *keys = [NSArray arrayWithObjects:[nextDict2 allKeys]];
			int i = 0;
			for (i = 0;i < [keys count];i++) {
				NSString *assiString = [[NSString alloc] initWithFormat:@"%@", [assignmentsArray objectAtIndex:indexPath.row]];
				if ([assiString isEqualToString:[NSString stringWithFormat:@"%@", [keys objectAtIndex:i]]]) {
					[nextDict2 removeObjectForKey:[NSString stringWithFormat:@"%@", [keys objectAtIndex:i]]];
					[startDict2 setObject:nextDict2 forKey:@"Assignments"];
					[[NSUserDefaults standardUserDefaults] setObject:startDict2 forKey:nameString];
					[[NSUserDefaults standardUserDefaults] synchronize];
				}
				[homeTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
				[assignmentsArray removeObjectAtIndex:indexPath.row];
				[homeTable reloadData];
			}
		}
	}
}
ipodtouchmaster05 is offline   Reply With Quote
Old 12-20-2008, 03:52 PM   #2 (permalink)
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

Based on my experiences, take out the "reloadData". I had problems with reloadData causing things to crash during table editing. Basically, update your data model and the table in parallel and just leave it at that. Seems to me that it's a bug.
__________________
For a little fun, check out my Biorhythms app
rames44 is offline   Reply With Quote
Old 12-20-2008, 03:54 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 180
Default

Quote:
Originally Posted by rames44 View Post
Based on my experiences, take out the "reloadData". I had problems with reloadData causing things to crash during table editing. Basically, update your data model and the table in parallel and just leave it at that. Seems to me that it's a bug.
thanks, but I tried that and it still crashes
ipodtouchmaster05 is offline   Reply With Quote
Old 12-20-2008, 04:03 PM   #4 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

If you are calling 'deleteRowsAtIndexPaths:' there is no reason to call 'reloadData'.

Since you are deleting multiple rows, wrap the whole 'for loop' with calls to the table methods 'beginUpdates' and 'endUpdates'. This tells the table you are about to do a bunch of changes.
RickMaddy is offline   Reply With Quote
Old 12-20-2008, 06:17 PM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

There are a number of memory leaks and other oddnesses in this code.

Objects created with mutableCopy must be released, yours are not. Objects created with alloc/init, like your assiString, must be released, yours is not.

This line is wrong and will crash:

Code:
NSArray *keys = [NSArray arrayWithObjects:[nextDict2 allKeys]];
arrayWithObjects is a varargs function that expects a nil terminated list of objects, you've only passed a single object, so it will crash. It is also wrong because there is no need to convert one array into another array, which seems to be what you're trying to do. This would be the usual way to get an array of the keys:

Code:
NSArray* keys = [nextDict2 allKeys];
There is no need to convert a string into another string with stringWithFormat. Code like the following is just weird:

Code:
NSString *assiString = [[NSString alloc] initWithFormat:@"%@", [assignmentsArray objectAtIndex:indexPath.row]];
Try this instead:

Code:
NSString *assiString = [assignmentsArray objectAtIndex:indexPath.row];
You don't need to release the string in this case.

I would not have the synchronize call inside the loop.
PhoneyDeveloper is offline   Reply With Quote
Old 12-20-2008, 07:29 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 180
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
There are a number of memory leaks and other oddnesses in this code.

Objects created with mutableCopy must be released, yours are not. Objects created with alloc/init, like your assiString, must be released, yours is not.

This line is wrong and will crash:

Code:
NSArray *keys = [NSArray arrayWithObjects:[nextDict2 allKeys]];
arrayWithObjects is a varargs function that expects a nil terminated list of objects, you've only passed a single object, so it will crash. It is also wrong because there is no need to convert one array into another array, which seems to be what you're trying to do. This would be the usual way to get an array of the keys:

Code:
NSArray* keys = [nextDict2 allKeys];
There is no need to convert a string into another string with stringWithFormat. Code like the following is just weird:

Code:
NSString *assiString = [[NSString alloc] initWithFormat:@"%@", [assignmentsArray objectAtIndex:indexPath.row]];
Try this instead:

Code:
NSString *assiString = [assignmentsArray objectAtIndex:indexPath.row];
You don't need to release the string in this case.

I would not have the synchronize call inside the loop.
thanks for the suggestions, but I tried them and it still crashes, but it ha an error now but i cant find out what it means so here is my code:

Code:
- (void) cancelPressed {
	HWPlannerAppDelegate *appDelegate = (HWPlannerAppDelegate*)[[UIApplication sharedApplication] delegate];
	[appDelegate resetAll];
	
	
	NSMutableDictionary *startDict2 = [NSMutableDictionary dictionaryWithDictionary:[[[NSUserDefaults standardUserDefaults] objectForKey:nameString] mutableCopy]];
	NSMutableDictionary *nextDict2 = [NSMutableDictionary dictionaryWithDictionary:[[startDict2 objectForKey:@"Assignments"] mutableCopy]];
	
	if ([nextDict2 count] > 0) {
		NSArray *keys = [nextDict2 allKeys];
		NSString *assiString = [NSString stringWithFormat:@"%@", [nameAndDesc objectAtIndex:0]];

		int k = 0;
		for (k = 0;k < [keys count];k++) {
			if ([assiString isEqualToString:[NSString stringWithFormat:@"%@", [keys objectAtIndex:k]]]) {
				[nextDict2 removeObjectForKey:[NSString stringWithFormat:@"%@", [keys objectAtIndex:k]]];
			}
		}
		
		[startDict2 setObject:nextDict2 forKey:@"Assignments"];
		[[NSUserDefaults standardUserDefaults] setObject:startDict2 forKey:nameString];
		[[NSUserDefaults standardUserDefaults] synchronize];
	}
	
	[reloadTimer invalidate];
	[self removeFromSuperview];
}
and the error i am getting is:

Code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIButtonContent objectAtIndex:]: unrecognized selector sent to instance 0x54a860'
thanks
ipodtouchmaster05 is offline   Reply With Quote
Old 12-20-2008, 07:49 PM   #7 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

This runtime exception means that your code tried to send a message to an object that doesn't respond to that message. The message your code tried to send is objectAtIndex: The usual cause for this error is that you've failed to retain an object that needed to be retained. The object gets released and the memory block where that object was gets re-used. Your code then sends the message thinking the object is still there but the new object gets the message meant for the old object.

In this block of code there are three lines that have objectAtIndex on them. The first one messages an object called nameAndDesc, which isn't shown anywhere else in this code. I expect that that object is an ivar that hasn't been retained correctly.

Can you use the debugger to step through this code? If you step through the code you should be able to see which lines succeed and which ones fail. You should easily be able to see which line causes the runtime exception to be thrown.
PhoneyDeveloper is offline   Reply With Quote
Old 12-20-2008, 08:10 PM   #8 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
This runtime exception means that your code tried to send a message to an object that doesn't respond to that message. The message your code tried to send is objectAtIndex: The usual cause for this error is that you've failed to retain an object that needed to be retained.
I politely disagree

This error indicates that the message 'objectAtIndex' is being called on an object that doesn't respond to the message/method/selector. Usually, if you call a method on a released object your get some sort of "bad access" error.

In this case the error clearly shows a call to 'objectAtIndex' on something called a 'UIButtonContent' - whatever that is. Most likely you meant to call 'objectAtIndex' on an NSArray but you mistakenly called it on some sort of button type object.
RickMaddy is offline   Reply With Quote
Old 12-20-2008, 08:21 PM   #9 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 180
Default

Quote:
Originally Posted by RickMaddy View Post
I politely disagree

This error indicates that the message 'objectAtIndex' is being called on an object that doesn't respond to the message/method/selector. Usually, if you call a method on a released object your get some sort of "bad access" error.

In this case the error clearly shows a call to 'objectAtIndex' on something called a 'UIButtonContent' - whatever that is. Most likely you meant to call 'objectAtIndex' on an NSArray but you mistakenly called it on some sort of button type object.
thanks all! I got it working.
ipodtouchmaster05 is offline   Reply With Quote
Old 12-20-2008, 09:08 PM   #10 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Hi Rick,

I agree that

Quote:
the error clearly shows a call to 'objectAtIndex' on something called a 'UIButtonContent' - whatever that is. Most likely you meant to call 'objectAtIndex' on an NSArray but you mistakenly called it on some sort of button type object.
and I believe that this is because

Quote:
you've failed to retain an object that needed to be retained. The object gets released and the memory block where that object was gets re-used. Your code then sends the message thinking the object is still there but the new object gets the message meant for the old object.
UIButtonContent is an undocumented class. The OP didn't create one and then treat it as an NSArray. He created something else, presumably an NSArray, and failed to retain it. Then after it was released a UIButtonContent was created by the framework in the same block of memory and he messaged it.

There are other possible reasons to get the 'unrecognized selector' runtime error, like a bad type cast or even ignoring warnings but failing to retain an object is a very common cause of this error. Maybe the OP can tell us what fixed his bug.
PhoneyDeveloper is offline   Reply With Quote
Old 12-20-2008, 09:12 PM   #11 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 180
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
Hi Rick,

I agree that



and I believe that this is because



UIButtonContent is an undocumented class. The OP didn't create one and then treat it as an NSArray. He created something else, presumably an NSArray, and failed to retain it. Then after it was released a UIButtonContent was created by the framework in the same block of memory and he messaged it.

There are other possible reasons to get the 'unrecognized selector' runtime error, like a bad type cast or even ignoring warnings but failing to retain an object is a very common cause of this error. Maybe the OP can tell us what fixed his bug.
umm yea you were right, I was calling in my app delegate to return an array, then I wanted to use that array. but in my app delegate I didnt return an allocated array, so that was the problem.
ipodtouchmaster05 is offline   Reply With Quote
Old 12-20-2008, 10:58 PM   #12 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
There are other possible reasons to get the 'unrecognized selector' runtime error, ... but failing to retain an object is a very common cause of this error.
Hmmm. Interesting. I guess I've never experienced this cause myself. Yet another nuance of Objective-C to remember. Thanks.
RickMaddy is offline   Reply With Quote
Reply

Bookmarks

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: 251
22 members and 229 guests
@sandris, ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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