0

テーブルビューに画像をロードしてテーブルビューをスクロールしようとするたびに、シミュレーターでクラッシュが発生しますが、エラーは表示されません。これは、使用されているメモリが多すぎる可能性があります。

以下は、3 つのビューのうちの 1 つのコードです。

#import "ResultViewController.h"
#import "JobAddSiteViewController.h"
#import "SpecificAddViewController.h"
#import "JobAddSiteAppDelegate.h"
#import "JSONKit.h"


@implementation ResultViewController
@synthesize listData;
@synthesize listLocation;
@synthesize listPostDate;
@synthesize listLogo;
@synthesize listDescription;
@synthesize uiTableView;
@synthesize buttonPrev;
@synthesize buttonNext;

NSInteger *countPage = 1;

NSMutableArray *tempArray;
NSMutableArray *tempArray2;
NSMutableArray *tempArray3;
NSMutableArray *tempArray4;
NSMutableArray *tempArray5;



-(IBAction)done{

JobAddSiteViewController *second = [[JobAddSiteViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
[second release];

}

 -(void)loadData{


NSString *strURL2 = [NSString stringWithFormat:@"http://www.bestitjobs.co.uk/totaljobs.php", ""];
NSData *nsData2 = [NSData dataWithContentsOfURL:[NSURL URLWithString: strURL2]];
NSString *dataResult = [[NSString alloc] initWithData:nsData2 encoding:NSUTF8StringEncoding];



tempArray = [[NSMutableArray alloc] init];
tempArray2 = [[NSMutableArray alloc] init];
tempArray3 = [[NSMutableArray alloc] init];
tempArray4 = [[NSMutableArray alloc] init];
tempArray5 = [[NSMutableArray alloc] init];
NSString *strURL = [NSString stringWithFormat:@"http://www.bestitjobs.co.uk/appresults3.php?pg=%d", countPage];
NSData *nsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: strURL]];


NSDictionary *listDictionary = [nsData objectFromJSONData];
NSArray* people =[listDictionary objectForKey:@"jobs"];
for (NSDictionary *person in people) { 
    NSString *str = [NSString stringWithFormat:@"%@", [person valueForKey:@"position"]];
    NSString *str2 = [NSString stringWithFormat:@"%@", [person valueForKey:@"subcounty"]];
    NSString *str3 = [NSString stringWithFormat:@"%@", [person valueForKey:@"postdate"]];
    NSString *str4 = [NSString stringWithFormat:@"%@", [person valueForKey:@"logo"]];
    NSString *str5 = [NSString stringWithFormat:@"%@", [person valueForKey:@"description"]];

    if(![str isEqualToString:@"<null>"])
    { 
        NSString *position = [person objectForKey:@"position"];
        [tempArray addObject: position];


        if(![str2 isEqualToString:@"<null>"])
        { 
            NSString *subcounty = [person objectForKey:@"subcounty"];
            [tempArray2 addObject: subcounty];
        }
        else{
            [tempArray2 addObject: @"-"];
        }

        if(![str3 isEqualToString:@"<null>"])
        { 
            NSString *postDate = [person objectForKey:@"postdate"];
            [tempArray3 addObject: postDate];
        }
        else{
            [tempArray3 addObject: @"-"];
        }

        if(![str4 isEqualToString:@"<null>"])
        { 
            NSString *logo = [person objectForKey:@"logo"];
            [tempArray4 addObject: [NSString stringWithFormat:@"http://www.bestitjobs.co.uk/employers/logo/Files/%@", logo]];
        }
        else{
            [tempArray4 addObject: [NSString stringWithFormat:@"http://www.bestitjobs.co.uk/employers/logo/Files/%@", "noimage.gif"]];
        }


        if(![str5 isEqualToString:@"<null>"])
        { 
            NSString *description = [person objectForKey:@"description"];
            [tempArray5 addObject: description];
        }
        else{
            [tempArray5 addObject: @"-"];
        }
    }


}



if (countPage == 1) {
    [self.buttonPrev setEnabled:FALSE];

}
else {

    [self.buttonPrev setEnabled:TRUE];
}


NSInteger val = [dataResult intValue];
NSInteger pageEnd = val/10;

if (countPage < pageEnd) {
    [self.buttonNext setEnabled:TRUE];

}
else {
    [self.buttonNext setEnabled:FALSE];
}


//NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod",@"iPad",nil];

self.listData = tempArray;
self.listLocation = tempArray2;
self.listPostDate = tempArray3;
self.listLogo = tempArray4;
self.listDescription = tempArray5;


[self.listData release];
[self.listLocation release];
[self.listPostDate release];
[self.listLogo release];
[self.listDescription release];

tempArray = nil;
tempArray2 = nil;
tempArray3 = nil;
tempArray4 = nil;
tempArray5 = nil;

} 

- (void)viewDidLoad {

[self loadData];
[super viewDidLoad];

}



- (void)dealloc {


[tempArray dealloc];
[tempArray2 dealloc];
[tempArray3 dealloc];
[tempArray4 dealloc];
[tempArray5 dealloc];

[self.listData dealloc];
[self.listLocation dealloc];
[self.listPostDate dealloc];
[self.listLogo dealloc];
[self.listDescription dealloc];
[super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods



- (IBAction)prev{

countPage = countPage - 1;

[self.listData removeAllObjects];
[self.listLocation removeAllObjects];
[self.listPostDate removeAllObjects];
[self.listLogo removeAllObjects];

//NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"1", @"2",@"3",nil];

//self.listData = array;
[self loadData];

[self.uiTableView reloadData];
}

- (IBAction)next{


countPage = countPage + 1;

[self.listData removeAllObjects];
[self.listLocation removeAllObjects];
[self.listPostDate removeAllObjects];
[self.listLogo removeAllObjects];

//NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"1", @"2",@"3",nil];

//self.listData = array;
[self loadData];

[self.uiTableView reloadData];


} 

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

JobAddSiteAppDelegate *ja = (JobAddSiteAppDelegate *)[[UIApplication sharedApplication] delegate];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

for (UIView *view in  cell.contentView.subviews){                

    if ([view isKindOfClass:[UILabel class]]){

        UILabel *label = (UILabel *)view;

        if (label.tag == 1) {

            ja.jobText = label.text;
        }
        if (label.tag == 2) {

            ja.locationText = label.text;
        }
        if (label.tag == 3) {

            ja.dateText = label.text;
        }
        if (label.tag == 4) {

        }
        if (label.tag == 5) {

            ja.specificText = label.text;
        }

    }
    if ([view isKindOfClass:[UIImageView class]]){

        UIImageView *image = (UIImageView *)view;

        if (image.tag = 4){

            ja.logoText = image.image;

        }

    }
}

SpecificAddViewController *second = [[SpecificAddViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
[second release];


}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath       
*)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) {
    return 65;
}
return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

UILabel *labelMain;
UILabel *labelLocation;
UILabel *labelDate;
UIImageView *image;
UILabel *ref;


if (cell == nil) {
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];


    image = [[[UIImageView alloc] initWithFrame:CGRectMake(0,3,80,62)] autorelease];
    image.tag = 4;
    [cell.contentView addSubview:image];


    labelMain = [[[UILabel alloc] initWithFrame:CGRectMake(90,3,200,20)] autorelease];
    labelMain.tag = 1;
    labelMain.font = [UIFont systemFontOfSize:14.0];
    [cell.contentView addSubview:labelMain];


    labelLocation = [[[UILabel alloc] initWithFrame:CGRectMake(90,20,200,20)] autorelease];
    labelLocation.tag = 2;
    labelLocation.font = [UIFont systemFontOfSize:12.0];
    labelLocation.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:labelLocation];


    labelDate = [[[UILabel alloc] initWithFrame:CGRectMake(90,40,200,20)] autorelease];
    labelDate.tag = 3;
    labelDate.font = [UIFont systemFontOfSize:12.0];
    labelDate.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:labelDate];

    ref = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
    ref.tag = 5;
    [cell.contentView addSubview:ref];
} 


[(UILabel *)[cell.contentView viewWithTag:1] setText:[self.listData objectAtIndex:indexPath.row]];
[(UILabel *)[cell.contentView viewWithTag:2] setText:[self.listLocation objectAtIndex:indexPath.row]];
[(UILabel *)[cell.contentView viewWithTag:3] setText:[self.listPostDate objectAtIndex:indexPath.row]];
[(UILabel *)[cell.contentView viewWithTag:5] setText:[self.listDescription objectAtIndex:indexPath.row]];
NSString *imagePath = [self.listLogo objectAtIndex:indexPath.row];
image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]]];


return cell;

}



@end
4

2 に答える 2

1

呼び出し

image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]]];

実行スレッドをブロックし、ネットワークの使用に関して不確定に長くなることは悪いことです。各セルは、画像が読み込まれるまで待つ必要があります。

研究トピックとして「tableViewセルの遅延読み込み」を確認してください。

代わりに、セルへのURLを指定し、メインスレッドから画像をロードするように指示する必要があります。

のように

[cell loadImageAtURL:someURL];

およびUITableViewCellサブクラス実装内

-(void)loadImageAtURL:(NSURL *)aurl
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        NSData *data = [NSData dataWithContentsOfURL:aurl];
        if (data) {
            UIImage *image = [UIImage imageWithData:data];

            //must update UI on main queue
            dispatch_async(dispatch_get_main_queue()  ,^{
                self.cellImageView = image;
            }
        }
    });

}

たくさんのObj-Cイメージローダーもあります。EGOCacheは私の図書館に行くものですが、よく見てください。

要約すると、セルは、画像が読み込まれて表示される前にセルが再利用されないという保証がないため、テーブルビューではなく画像の読み込みプロセスを所有する必要があります。

于 2012-12-12T15:11:09.670 に答える
0

NSInteger はプリミティブ型です。つまり、スタックにローカルに格納できます。アクセスするためにポインターを使用する必要はありません。あなたがそれを使用している方法は、プリミティブ型の実際の値ではなくポインター値を取得するという問題だと思います。

これはcountPageの宣言/初期化に必要なものだと思います:

NSInteger countPage = 1;
于 2012-12-12T14:08:24.877 に答える