views:

36

answers:

1

I'm a totally noob in IPhone development. Just start a week ago. I'm trying to have this tableview working.

I have a class I created called CustomerRepository with a method like this

 - (CustomerRepository *) init {

self = [super init];

if (self) {


    customerArray = [[NSMutableArray alloc] init];

    Customer *cust1 = [[Customer alloc] init];
    cust1.name = @"cust1";  

    [customerArray addObject: cust1];

    [cust1 release];
}


return self;

}

- (NSMutableArray *) GetAll {

    NSMutableArray *returnCustomerArray = [[[NSMutableArray alloc] init] autorelease]; 

    for(Customer *cust in customerArray)
    {
        Customer *copy = [[Customer alloc]init];
            copy.name = cust.name;
        [returnCustomerArray addObject:copy];
        [copy release];
    }

    return returnCustomerArray;

}

Now In my Controller

  @synthezise customerArray;

  viewDidLoad {

    CustomerRepository *custRepo = [[CustomerRepository alloc] init];

customerArray = [custRepo GetAll];

[custRepo release];
  }


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

    // It always throw an exception here about -[UITableViewRowData count]: unrecognized selector sent to instance
    return [customerArray count];
}

There is definitely something wrong with my code, can you guys help me point out what is wrong. Most probably an access to an instance that is already release ....

+2  A: 

You need to retain the array returned by GetAll. Try this:

viewDidLoad {

  CustomerRepository *custRepo = [[CustomerRepository alloc] init];

  customerArray = [[custRepo GetAll] retain];

  [custRepo release];
}

If you don't retain it, then your autorelease in -GetAll means that the returned array will eventually be released. When your -numberOfRowsInSection method fires, it's talking to a dealloc'd instance.

Ben Gottlieb
@Ben, I was literally typing the same exact answer! +1
Jacob Relkin
Heh, sorry 'bout that! +1 back atcha then :-)
Ben Gottlieb
I tried that before posting but its still the same error coming up ...
pdiddy