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:
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.
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'
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'
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
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.
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?
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.