2009-10-20 8 views
0

배열에 포함 된 항목을 표시하기 위해 UITableView를 가져 오려고합니다. 이 배열은 클래스 (HX_ParkingSearch) 내에 포함되어 있습니다.스크롤했을 때 UITableView가 손상되었습니다.

뷰를 액세스 할 수 있도록 앱 위임 클래스 내에 배열을 포함하는이 클래스에 대한 참조가 있습니다. 문제는 한 페이지의 결과가 테이블 뷰 에 올바르게 표시되지만, 배열의 다음 항목에 액세스하려고 할 때 예외가 발생하면 스크롤을 시도 할 때 발생합니다. 내가 아래로 스크롤하고 cellForRowAtIndexPath 메서드가 실행되면 배열 내의 항목이 유효하지 않고 해제 된 것처럼 보입니다. 그러나 내가 이해하지 못하는 부분은 입니다.

아무도 아이디어가 없습니까? 내 머리가 정말 지금하고 있습니다.

많은 감사, Chris.

// 표보기 셀의 모양을 사용자 정의하십시오.

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

답변

0

loadLocations에서 30 개의 용량으로 배열을 초기화하지만 15 개의 항목 만 삽입해야하는 이유가 있습니까? 당신의 데이터 소스? 또한

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

및이 방법에 무엇이

+0

이유가 없습니다. – Chris

0

:

있는 tableview 단지 위치의 경계에서의 인덱스를 잡으려고 할 수있다

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

+0

안녕하세요. 답장을 보내 주셔서 대단히 감사드립니다. 지난 밤에 작업을 마쳤습니다! 분명히 너무 오랫동안 머리카락을 당기는 데에 썼다. HXParkingLocation의 콘텐츠에 대한 참조를 유지하지 못했기 때문에 모든 콘텐츠가 공개되었습니다! HXParking 위치에 대한 실제 참조는 괜찮 았고 나는 분명히 막대기의 잘못된 끝을 가지고있었습니다! 어쨌든 건배, 가장 높이 평가합니다. Chris. – Chris

관련 문제