【问题标题】:UITableView corruption upon scroll滚动时 UITableView 损坏
【发布时间】:2009-10-20 15:40:50
【问题描述】:

我试图让 UITableView 显示包含在数组中的项目。 该数组包含在一个类 (HX_ParkingSearch) 中。

我有一个包含应用程序委托类中的数组的此类的引用,以使视图能够访问它。 问题是我在表格视图中正确显示一页结果 但是当我尝试向下滚动时,尝试访问数组中的下一项时会发生异常。 事实证明,当我向下滚动并触发 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];

}

【问题讨论】:

  • 如果不是存储 HX_ParkingLocation 我将 NSString 类型的对象存储在数组中,似乎不会出现问题!任何想法为什么会这样?干杯,克里斯

标签: iphone objective-c uitableview nsmutablearray


【解决方案1】:

您是否有理由在 loadLocations 中初始化容量为 30 的数组,但只插入 15 个项目?

【讨论】:

  • 没有理由我只是在玩。
【解决方案2】:

这个方法对你的数据源有什么作用?:

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

还有:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

tableview 可能只是试图获取超出位置范围的索引

【讨论】:

  • 您好,非常感谢您的回复。昨晚深夜设法解决了!显然,在没有休息的情况下拉头发的时间太长了。原来我没有保留对 HXParkingLocation 内容的引用,所以所有内容都被释放了! HXParking 位置的实际参考很好,我显然弄错了!无论如何,干杯,非常感谢。克里斯。
猜你喜欢
  • 2023-04-02
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
相关资源
最近更新 更多