Reputation: 691
I am passing a mutable array over from another viewcontroller by saving it in nsuserdefaults. I then retrieve it in the new viewcontroller in which it nslogs just fine. I run into a problem when my code reaches numberofrowsinsections. It seems like my tableview is not recognizing my array object. Thanks for any help as I am new at coding.
#import "ViewController.h"
#import "RecordView.h"
#import "bookCellTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize arr;
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *arr = [[NSMutableArray alloc]init];
arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"projects"];
NSLog(@"arr%@", [arr description]);
#pragma mark - Table view data source
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"Cell";
bookCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ToBook"];
if(cell != nil)
{
cell.songTitle.text = [arr objectAtIndex:indexPath.row];
testLabel.text = @"Hello";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [bookView cellForRowAtIndexPath:indexPath];
}
@end
Upvotes: 0
Views: 93
Reputation: 45490
It's a scope issue:
You have declared your datasource in viewDidLoad
NSMutableArray *arr = [[NSMutableArray alloc]init];
That's not correct, your datasource should be a property , or a variable on class scope at least.
You should initialize it in viewDidLoad
but declare it on the class level.
It seems like you have one property called arr
already, a smart compiler should warn you that is ambiguous.
To fix this just remove NSMutableArray *
part of that line, just like this:
arr = [[NSMutableArray alloc]init];
You are also misusing NSUserdefault
it is not meant to pass data between controller, it is more appropriate for storing basic user settings values.
To pass data, simply set values on the ViewController
properties before presenting it.
Upvotes: 1