2009-10-20 92 views
0

我试图让UITableView显示数组中包含的项目。 此数组包含在一个类(HX_ParkingSearch)中。滚动时UITableView损坏

我有一个ref这个类包含应用程序委托类内的数组,以使视图访问它。 问题是,我得到一个页面的结果正确显示在桌面视图 内,但是当我尝试向下滚动时试图访问数组中的下一个项目时发生异常。 事实证明,当我向下滚动和cellForRowAtIndexPath方法触发, 数组内的项目是无效的,似乎已被释放,但我不明白在哪里 他们正在释放!

有没有人有任何想法,因为这是真的在做我的脑袋!

非常感谢, 克里斯。

//自定义表格视图单元格的外观。

- (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

它似乎这个问题不会发生,而不是存储HX_ParkingLocation我将类型为NSString的对象存储在数组中! 任何想法,为什么这可能是? 干杯, chris – Chris 2009-10-20 16:20:41

回答

0

是否有一个原因,你在loadLocations中初始化容量为30的数组,但只能插入15个项目?

+0

没有理由我只是在玩耍。 – Chris 2009-10-20 16:06:21

0

什么在这个方法为您的数据源?:

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

也:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

的实现代码如下可能只是试图抓住这出位置界限的指数

+0

嗨,非常感谢您的回复。 管理工作昨晚深夜!显然,花太长的时间拉头发没有休息。 原来,我没有保留HXParkingLocation的内容,所以所有的内容都被释放! HX停车位的实际参考号很好,我明显得到了棒的错误结局! 干杯无论如何,最感谢。 Chris。 – Chris 2009-10-21 08:43:04