0

配列内に含まれるアイテムを表示するために UITableView を取得しようとしています。この配列は、クラス (HX_ParkingSearch) に含まれています。

ビューがアクセスできるようにするために、アプリデリゲートクラス内の配列を含むこのクラスへの参照があります。問題は、テーブルビュー内に 1 ページの結果が正しく表示されることですが、下にスクロールしようとすると、配列内の次の項目にアクセスしようとすると例外が発生します。下にスクロールして 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];

}
4

2 に答える 2

0

データソースのこのメソッドには何がありますか?:

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

また:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

テーブルビューは、場所の範囲外のインデックスを取得しようとしているだけかもしれません

于 2009-10-20T21:11:12.520 に答える
0

loadLocations で 30 の容量で配列を初期化しているのに、15 項目しか挿入していない理由はありますか?

于 2009-10-20T15:55:58.553 に答える