0

每次我在表格视图中加载图像并尝试滚动表格视图时,我的模拟器都会崩溃,但没有显示错误。这可能是因为使用了太多内存。

以下是三个视图中的 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 回答