views:

93

answers:

2

Hello all, Im trying to get a UITableView to display items contained within an array. This array is contained within a class (HX_ParkingSearch).

I have a ref to this class that contains the array inside the app delegate class, to enable the views to access it. Problem is that I get one page of results displaying correctly inside the tableview but when i try and scroll down an exception occurs when trying to access the next item in the array. It turns out that when i scroll down and the cellForRowAtIndexPath method fires, the items inside the array are invalid and appear to have been released but i dont understand where they are being released!

Does anyone have any ideas because this is really doing my head in now!

Many thanks, Chris.

// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
HX_ParkingLocation *location;
bookingApp2AppDelegate *del  = (bookingApp2AppDelegate *) [[UIApplication sharedApplication] delegate];   


NSMutableArray* array = [del.parkingSearch locations];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


location = (HX_ParkingLocation*) [array objectAtIndex: [indexPath row] ];




return cell;

}

import

@interface HX_ParkingLocation : NSObject {

NSString *name;

}

@property(retain,nonatomic) NSString* name;

/* Initialises this Location instance by passing in the name and code of the location and the URL of the webapi product endpoint. The URL is used to find available products at this location. / -(id) initWithName: (NSString) n;

@end

import

@interface HX_ParkingSearch : NSObject { NSMutableArray* locations;

} @property (retain) NSMutableArray* locations;

-(BOOL) loadLocations;

@end

import "HX_ParkingSearch.h"

import "HX_Parking_Location.h"

@implementation HX_ParkingSearch @synthesize locations;

//Finds the locations -(BOOL) loadLocations {

[locations release];
//Create array to hold locations
locations = [[NSMutableArray alloc] initWithCapacity:30];

//Loop through all returned locations
for(int i=0;i<15;i++)
{
 //Get location name
 NSString* n = [NSString stringWithFormat:@"Item #%i",i ];



 //Create location instance, which retrieves availability and product information and stores the information in location object.
 HX_ParkingLocation* location = [[HX_ParkingLocation alloc] initWithName:n];
 //add to array
  [locations addObject:location];




}


return YES;

}

@end

import

import "HX_ParkingSearch.h"

@interface bookingApp2AppDelegate : NSObject {

UIWindow *window;
UINavigationController *navigationController;
 HX_ParkingSearch *parkingSearch;

}

@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @property (retain) HX_ParkingSearch *parkingSearch;

@end

@implementation bookingApp2AppDelegate

@synthesize window; @synthesize navigationController; @synthesize parkingSearch;

  • (void)applicationDidFinishLaunching:(UIApplication *)application {

    //Create new parking search instance by specifying the endpoint urls HX_ParkingSearch* search = [[HX_ParkingSearch alloc] init]; [search loadLocations];

    parkingSearch = search; //NSLog(@"Search Retain count = %i" ,[search retainCount]);

    [window addSubview:[navigationController view]]; //[window addSubview:[navigationController initWithNibName:@"VC_Locations" bundle:[NSBundle mainBundle]]]; [window makeKeyAndVisible];

}

A: 

Is there a reason that you're init-ing your array with a capacity of 30 in loadLocations, but only inserting 15 items?

Justin Gallagher
No reason i was just playing around.
Chris
A: 

what's in this method for your datasource?:

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

and also:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

the tableview may just be trying to grab an index that's out of bounds of locations

pxl
Hi thanks very much for replying.Managed to work it out late last night! Obviously too long spent pulling hair w/o a break.Turns out that I wasnt retaining a ref to the contents of HXParkingLocation so all the contents were being released!The actual ref to HXParking location was fine and i had obviously got the wrong end of the stick!Cheers anyway, most appreciated.Chris.
Chris