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 09-04-2010, 11:53 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default Table View Update

Hey everyone!

I'm having some trouble updating my tableView. I have my table populated with an array and i'm also using the objectAtIndex function no problem to make my array 2 dimensional. The issue comes in where i'm taking text from a UITextField, storing it into a string, and then storing that into the array. From there, through a button, i'm trying to edit the values of the array and then update my view. How would I go about doing this? I have the following code for my button:

Code:
- (IBAction)termNameConfirm:(id)sender {
	
	NSString *termName = [[NSString alloc] init];
	termName = termNameField.text;
	[test insertObject:termName atIndex:0];
	
	[self.tableView reloadData];
}
However, it keeps crashing when I click the button. Any suggestions?
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 12:03 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Any output from the console when it crashes?

You're also leaking termName. First you assign it to a newly allocated NSString, but then you assign it to termNameField.text. You've then lost your pointer to the new NSString but it's still in memory, so it's leaking.

Code:
- (IBAction)termNameConfirm:(id)sender {
	[test insertObject:termNameField.text atIndex:0];
	[self.tableView reloadData];
}
That will clear up the leak, as for the crash, post the output from the console.
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 12:14 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

I saved the change so it looks cleaner. Here is the output after the crash:

Code:
[Session started at 2010-09-04 13:11:59 -0400.]
2010-09-04 13:12:07.269 SchoolOne[2086:207] -[NSCFString count]: unrecognized selector sent to instance 0x5d55a20
2010-09-04 13:12:07.272 SchoolOne[2086:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x5d55a20'
*** Call stack at first throw:
(
	0   CoreFoundation                      0x0239a919 __exceptionPreprocess + 185
	1   libobjc.A.dylib                     0x024e85de objc_exception_throw + 47
	2   CoreFoundation                      0x0239c42b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
	3   CoreFoundation                      0x0230c116 ___forwarding___ + 966
	4   CoreFoundation                      0x0230bcd2 _CF_forwarding_prep_0 + 50
	5   SchoolOne                           0x000034b6 -[TermSelectViewController tableView:numberOfRowsInSection:] + 83
	6   UIKit                               0x00473a24 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
	7   UIKit                               0x004759c1 -[UITableViewRowData rectForFooterInSection:] + 108
	8   UIKit                               0x0047524d -[UITableViewRowData heightForTable] + 60
	9   UIKit                               0x00338596 -[UITableView(_UITableViewPrivate) _updateContentSize] + 333
	10  UIKit                               0x00327b7e -[UITableView noteNumberOfRowsChanged] + 123
	11  UIKit                               0x003341d2 -[UITableView reloadData] + 773
	12  SchoolOne                           0x0000385c -[TermSelectViewController termNameConfirm:] + 153
	13  UIKit                               0x002bee14 -[UIApplication sendAction:to:from:forEvent:] + 119
	14  UIKit                               0x003486c8 -[UIControl sendAction:to:forEvent:] + 67
	15  UIKit                               0x0034ab4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
	16  UIKit                               0x003496f7 -[UIControl touchesEnded:withEvent:] + 458
	17  UIKit                               0x002e22ff -[UIWindow _sendTouchesForEvent:] + 567
	18  UIKit                               0x002c41ec -[UIApplication sendEvent:] + 447
	19  UIKit                               0x002c8ac4 _UIApplicationHandleEvent + 7495
	20  GraphicsServices                    0x02c00afa PurpleEventCallback + 1578
	21  CoreFoundation                      0x0237bdc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
	22  CoreFoundation                      0x022dc737 __CFRunLoopDoSource1 + 215
	23  CoreFoundation                      0x022d99c3 __CFRunLoopRun + 979
	24  CoreFoundation                      0x022d9280 CFRunLoopRunSpecific + 208
	25  CoreFoundation                      0x022d91a1 CFRunLoopRunInMode + 97
	26  GraphicsServices                    0x02bff2c8 GSEventRunModal + 217
	27  GraphicsServices                    0x02bff38d GSEventRun + 115
	28  UIKit                               0x002ccb58 UIApplicationMain + 1160
	29  SchoolOne                           0x000022c8 main + 102
	30  SchoolOne                           0x00002259 start + 53
)
terminate called after throwing an instance of 'NSException'
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 12:15 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Post you code that's in tableView:numberOfRowsInSection:
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 12:20 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

Here ya go. Thanks again!

Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [[test objectAtIndex:0] count];
}
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 12:23 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

I'll break what you're doing down into two parts.
Code:
NSString * tempString = [test objectAtIndex:0];
NSInteger tempCount = [tempString count];
Unfortunately, NSString doesn't have a -count method. What you want to be doing it getting the array count.

Code:
return [test count];
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 01:05 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

I made the change so my button works perfectly. Only problem now is it's crashing when I revert back to the table view.

Code:
Session started at 2010-09-04 14:03:56 -0400.]
2010-09-04 14:04:03.810 SchoolOne[2289:207] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x592c7b0
2010-09-04 14:04:03.812 SchoolOne[2289:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x592c7b0'
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 01:25 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Post the places where you're using objectAtIndex, according to the crash log, you're calling it on an NSString not an NSArray.
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 01:30 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell..
	NSUInteger index = [indexPath row];
	[[cell textLabel] setText:[[test objectAtIndex:0] objectAtIndex:index]];
    return cell;
}
That's where I use it. It crashes mainly when after I'm trying to call dismissModalViewControllerAnimation to remove a presentModalViewController.

Code:
- (IBAction)termAddAnimationDismiss:(id)sender {

	[gradeSummaryNavigation dismissModalViewControllerAnimated:YES];

}
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 05:31 PM   #10 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You should just be using objectAtIndex once, not twice.
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 07:25 PM   #11 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

That seems to fix the problem in console but now I get this error:

Code:
Session started at 2010-09-04 20:23:58 -0400.]
2010-09-04 20:24:01.816 SchoolOne[3065:207] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5927540
2010-09-04 20:24:01.819 SchoolOne[3065:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5927540'
However, I never used isEqualToString.
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 07:47 PM   #12 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Puzzling, are you doing any kind of string comparison anywhere?
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 07:54 PM   #13 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

I just decided to post all the code here lol. I'm basically trying to have a 2 dimensinal array where I can add in values from a text field. The reason why I was using two objectAtIndex was because i'm trying to specify where I want to place the value. For example,

[test objectAtIndex:0] objectAtIndex:0]

(First row, first column)

Code:
//
//  TermSelectViewController.m
//  SchoolOne
//
//  Created by Shane Zaman on 10-09-03.
//  Copyright 2010 Zaman Creations. All rights reserved.
//

#import "TermSelectViewController.h"
#import "SchoolOneAppDelegate.h"
#import "FirstViewController.h"


@implementation TermSelectViewController

@synthesize gradeSummaryNavigation;

@synthesize gradeSummary;
@synthesize termAddView;

@synthesize termNameButton;

@synthesize termAddButton;
@synthesize termCancelButton;

@synthesize termNameField;

@synthesize termSelect;
@synthesize gradeSelect;
@synthesize test;


#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle




- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	termCount = 0;
	termCount2 = 0;
	
	if (termCount2 == 0) {
		test = [[NSMutableArray alloc] initWithCapacity:5];
		[test insertObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil] atIndex:0];
		
		termCount2 = termCount2 + 1;
	}

	
	
	
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	[self.tableView reloadData];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [test count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell..
	NSUInteger index = [indexPath row];
	NSString *testString = [[NSString alloc] init];
	testString = [[test objectAtIndex:0] objectAtIndex:0];
	[[cell textLabel] setText:testString];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
	
	[gradeSummaryNavigation pushViewController:gradeSummary animated:YES];
}

- (IBAction)termAddAnimation:(id)sender {
	
	
	
	if (termCount == 0) {
		termAddView = [[UINavigationController alloc] initWithRootViewController:termAddView];
		termAddView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
		
		termCount = termCount + 1;
	}
	
	[gradeSummaryNavigation presentModalViewController:termAddView animated:YES];
	
}

- (IBAction)termAddAnimationDismiss:(id)sender {

	[gradeSummaryNavigation dismissModalViewControllerAnimated:YES];

}

- (IBAction)termNameConfirm:(id)sender {
	
	[test insertObject:termNameField.text atIndex:0];
	
	[self.tableView reloadData];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 07:58 PM   #14 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You're leaking again here:
Code:
NSString *testString = [[NSString alloc] init];
testString = [[test objectAtIndex:0] objectAtIndex:0];
It should just be:
Code:
NSString *testString = [[test objectAtIndex:0] objectAtIndex:0];
But I can't see the source of the crash.
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 08:03 PM   #15 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

I know it's really weird. When I run the application, the first row shows the letter "a" which is stored in the view did load. Then i use presentModalViewController to pop up a window with a text field and button. The use enters a letter (no spaces) and hits the button. Everything is fine. But then when the user clicks the other button to close the view, using dismissModalViewController, the program crashes.

Here is the error console:

Code:
[Session started at 2010-09-04 21:00:13 -0400.]
2010-09-04 21:00:25.473 SchoolOne[3487:207] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d47fd0
2010-09-04 21:00:25.475 SchoolOne[3487:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d47fd0'
*** Call stack at first throw:
(
	0   CoreFoundation                      0x0239a919 __exceptionPreprocess + 185
	1   libobjc.A.dylib                     0x024e85de objc_exception_throw + 47
	2   CoreFoundation                      0x0239c42b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
	3   CoreFoundation                      0x0230c116 ___forwarding___ + 966
	4   CoreFoundation                      0x0230bcd2 _CF_forwarding_prep_0 + 50
	5   SchoolOne                           0x000035b8 -[TermSelectViewController tableView:cellForRowAtIndexPath:] + 258
	6   UIKit                               0x0032ea3f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
	7   UIKit                               0x00324ad2 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
	8   UIKit                               0x0033940c -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
	9   UIKit                               0x003314bc -[UITableView layoutSubviews] + 242
	10  QuartzCore                          0x0402a0d5 -[CALayer layoutSublayers] + 177
	11  QuartzCore                          0x04029e05 CALayerLayoutIfNeeded + 220
	12  QuartzCore                          0x0402964c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
	13  QuartzCore                          0x040292b0 _ZN2CA11Transaction6commitEv + 292
	14  QuartzCore                          0x04030f5b _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
	15  CoreFoundation                      0x0237bd1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
	16  CoreFoundation                      0x02310987 __CFRunLoopDoObservers + 295
	17  CoreFoundation                      0x022d9c17 __CFRunLoopRun + 1575
	18  CoreFoundation                      0x022d9280 CFRunLoopRunSpecific + 208
	19  CoreFoundation                      0x022d91a1 CFRunLoopRunInMode + 97
	20  GraphicsServices                    0x02bff2c8 GSEventRunModal + 217
	21  GraphicsServices                    0x02bff38d GSEventRun + 115
	22  UIKit                               0x002ccb58 UIApplicationMain + 1160
	23  SchoolOne                           0x000022e0 main + 102
	24  SchoolOne                           0x00002271 start + 53
)
terminate called after throwing an instance of 'NSException'
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 08:07 PM   #16 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

The problem is that sometimes, the objectAtIndex: is being called on an array, sometimes it's being called on an NSString.

You can check by using:
Code:
id object = [test objectAtIndex:indexPath.row];
if ([object isKindOfClass:[NSString class]]){
    [cell.textLabel setText:object];
}
else
{
    [cell.textLabel setText:[object objectAtIndex:0]];
}
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 08:12 PM   #17 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

You are God LOL. Wow that worked perfectly. I really do appreciate the help. I have one more small request. Could you please explain the first two lines just so I understand what it's doing exactly?
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 08:17 PM   #18 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Sure, first of all, you're referencing whatever object is in the test array on the first dimension, whether it be an NSString or an NSArray. I would say using "id object" is almost the same as "NSObject * object". So it doesn't matter what type the object is, we're still referencing it.

The second line then check what kind of object we're referencing, "isKindOfClass:". If it's an NSString, we take the action of assigning it to the cell text, if not, we can assume it's an NSArray, and go that one level deeper.

Hope that helps.
harrytheshark is offline   Reply With Quote
Old 09-04-2010, 08:20 PM   #19 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

Genius. I really do appreciate the help once again. Thank you harry!
Coolio098 is offline   Reply With Quote
Old 09-04-2010, 08:21 PM   #20 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

No problem
harrytheshark 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: 346
11 members and 335 guests
bignoggins, carlandrews, flamingliquid, hzwegjxg, ilmman, iram91419, linkmx, nadav@webtview.com, Objective Zero, stanny, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

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