2009-10-20 8 views
0

Je tente d'obtenir un UITableView pour afficher les éléments contenus dans un tableau. Ce tableau est contenu dans une classe (HX_ParkingSearch).UITableView corruption lors du défilement

J'ai une référence à cette classe qui contient le tableau dans la classe de délégué de l'application, pour permettre aux vues d'y accéder. Le problème est que j'obtiens une page de résultats s'affichant correctement dans la tableview mais quand j'essaye de faire défiler vers le bas une exception se produit en essayant d'accéder à l'article suivant dans le tableau. Il s'avère que lorsque je défile vers le bas et la méthode cellForRowAtIndexPath se déclenche, les éléments à l'intérieur du tableau ne sont pas valides et semblent avoir été libérés, mais je ne comprends pas où ils sont libérés!

Est-ce que quelqu'un a des idées parce que c'est vraiment ma tête maintenant! Un grand merci, Chris.

// Personnalisez l'aspect des cellules de vue de table.

- (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 <Foundation/Foundation.h> 


@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 <Foundation/Foundation.h> 


@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 <UIKit/UIKit.h> 
#import "HX_ParkingSearch.h" 

@interface bookingApp2AppDelegate : NSObject <UIApplicationDelegate> { 

    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]; 

} 
+0

Il semble que le problème ne se produit pas si au lieu de stocker HX_ParkingLocation je stocke des objets de type NSString dans le tableau! Des idées pourquoi ce pourrait être? Cheers, chris – Chris

Répondre

0

Y a-t-il une raison pour laquelle vous initiez votre tableau avec une capacité de 30 dans loadLocations, mais en insérant seulement 15 éléments?

+0

Pas de raison pour laquelle je ne faisais que jouer. – Chris

0

ce qui est dans cette méthode pour votre source de données ?:

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

et aussi:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

le tableview peut juste essayer de saisir un indice qui est hors des limites des emplacements

+0

Salut merci beaucoup d'avoir répondu. Géré pour travailler tard la nuit dernière! Évidemment trop longtemps passé à tirer les cheveux sans une pause. Il s'avère que je ne conservais pas une référence au contenu de HXParkingLocation donc tout le contenu a été publié! Le ref réel à l'emplacement HXParking était bien et j'avais évidemment eu la mauvaise extrémité du bâton! Bravo de toute façon, le plus apprécié. Chris. – Chris

Questions connexes