Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Samppaa

macrumors regular
Original poster
Hey, I got a little problem and I don't want to get stuck on it for all night so I decided to show it there, btw this is really a noob thing, but I am just learning from a book and some tutorials and decided to try to make something own this time, I just like this way of learning that you expiriment some things and ask for help when you really get stuck hard and have tried things etc... I don't want to constantly read a book, but to try things. So I was reading a tutorial, but it didn't show how I can add things to the tableview. I know that I have to add the item to that array and I made a function in my rootcontroller and linked to button, but how I can reload the table from controller so it shows on the list? Here is code for you guys:

RootViewController.h
Code:
//
//  RootViewController.h
//  TableViewTest
//
//  Created by Samuli Lehtonen on 31.5.2010.
//  Copyright Test 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
	NSMutableArray *listOfItems;
}

-(IBAction)AddItemToArray(id)sender;

@end

RootViewController.m
Code:
//
//  RootViewController.m
//  TableViewTest
//
//  Created by Samuli Lehtonen on 31.5.2010.
//  Copyright Test 2010. All rights reserved.
//

#import "RootViewController.h"


@implementation RootViewController


#pragma mark -
#pragma mark View lifecycle

-(IBAction)AddItemToArray(id)sender
{
	[listOfItems addObject:@"Finland"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	listOfItems = [[NSMutableArray alloc] init];
	
	[listOfItems addObject:@"IceLand"];
	[listOfItems addObject:@"Greenland"];
	[listOfItems addObject:@"Switzerland"];
	[listOfItems addObject:@"Norway"];
	[listOfItems addObject:@"New Zealand"];
	[listOfItems addObject:@"Greece"];
	[listOfItems addObject:@"Italy"];
	[listOfItems addObject:@"Ireland"];
	
	self.navigationItem.title = @"Countries";

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/
- (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

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listOfItems 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
	cell.text = cellValue;

    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:UITableViewRowAnimationFade];
    }   
    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 {
    
	/*
	 <#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];
	 */
}


#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 {
	[listOfItems release];
    [super dealloc];
}


@end

So what I mean is how do I reload the table to make the finland show there?
 
change your adding action to:
Code:
-(IBAction)AddItemToArray:(id)sender
{
	[listOfItems addObject:@"Finland"];
	NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:([listOfItems count] - 1) inSection:0];
	[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
}
This code should work out just fine, i've tested it on my simulator, in case something went wrong unexpectedly, just keep me updated.
 
Crashes with that code also:
[Session started at 2010-06-01 17:38:04 +0300.]
2010-06-01 17:38:08.622 TableViewTest[953:207] *** -[RootViewController AddItem:]: unrecognized selector sent to instance 0x391bf50
2010-06-01 17:38:08.624 TableViewTest[953:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[RootViewController AddItem:]: unrecognized selector sent to instance 0x391bf50'
2010-06-01 17:38:08.624 TableViewTest[953:207] Stack: (
29291611,
2440811785,
29673531,
29242998,
29095618,
2720773,
4634529,
2720773,
3128142,
3136879,
3132091,
2825695,
2734024,
2760801,
37387609,
29076352,
29072456,
37381653,
37381850,
2764719,
8256
)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.