如何在tableview中重新加载数据

时间:2012-12-11 12:54:53

标签: iphone xcode uitableview ios5

每次我在按钮内重新加载数据时,单击应用程序崩溃。 这是代码:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface ResultViewController : UIViewController <UITableViewDelegate,      
UITableViewDataSource> {
NSMutableArray *listData;
NSMutableArray *listLocation;
NSMutableArray *listPostDate;
NSMutableArray *listLogo;
IBOutlet UINavigationBar *uiNavigationBar;
IBOutlet UITableView *uiTableView;

}

@property (nonatomic, retain) NSMutableArray *listData;
@property (nonatomic, retain) NSMutableArray *listLocation;
@property (nonatomic, retain) NSMutableArray *listPostDate;
@property (nonatomic, retain) NSMutableArray *listLogo;
@property (nonatomic, retain) UITableView *uiTableView;

-(IBAction)done;
-(IBAction)prev;
-(IBAction)next;

@end

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

@implementation ResultViewController
@synthesize listData;
@synthesize listLocation;
@synthesize listPostDate;
@synthesize listLogo;

NSInteger *countPage = 1;

NSArray *rows;
NSMutableArray *jsonResults;

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

-(IBAction)done{

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

}

- (void)viewDidLoad {

JobAddSiteAppDelegate *ja = (JobAddSiteAppDelegate *)[[UIApplication sharedApplication] delegate];

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];
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"]];

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

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

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

    if(![str4 isEqualToString:@"<null>"])
    { 
        NSString *logo = [person objectForKey:@"logo"];
        [tempArray4 addObject: logo];
    }
}


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

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


//[tempArray release];
[listData release];
[listLocation release];
[listPostDate release];
[listLogo release];
[super viewDidLoad];

}


- (void)dealloc {
//[tempArray dealloc];
[listData dealloc];
[listLocation dealloc];
[listPostDate dealloc];
[listLogo dealloc];
[super dealloc];
}

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

- (IBAction)prev{


}

- (IBAction)next{

[listData addObject:@"Mac Mini"];
[listLocation addObject:@"Mac Mini"];
[listPostDate addObject:@"Mac Mini"];
[listLogo addObject:@"Mac Mini"];
UITableView *tv = (UITableView *)self.view;
[tv reloadData];

}

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


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(80,3,200,20)] autorelease];
    labelMain.tag = 1;
    labelMain.font = [UIFont systemFontOfSize:14.0];
    [cell.contentView addSubview:labelMain];


    labelLocation = [[[UILabel alloc] initWithFrame:CGRectMake(80,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(80,40,200,20)] autorelease];
    labelDate.tag = 3;
    labelDate.font = [UIFont systemFontOfSize:12.0];
    labelDate.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:labelDate];

} 


[(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]];
NSString *imagePath = [self.listLogo objectAtIndex:indexPath.row];
UIImage *uiImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"http://www.bestitjobs.co.uk/employers/logo/Files/%@", imagePath]];
//NSLog([NSString stringWithFormat:@"http://www.bestitjobs.co.uk/employers/logo/Files/%@", imagePath]);
image.image = uiImage;

return cell;

}



@end

错误

2012-12-11 12:35:14.828 JobAddSite [674:207] - [UIView reloadData]:无法识别的选择器发送到实例0x4e2d420 2012-12-11 12:35:14.931 JobAddSite [674:207] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIView reloadData]:无法识别的选择器发送到实例0x4e2d420' * 在第一次投掷时调用堆栈: (     0 CoreFoundation 0x00deb5a9 exceptionPreprocess + 185     1 libobjc.A.dylib 0x00f3f313 objc_exception_throw + 44     2 CoreFoundation 0x00ded0bb - [NSObject(NSObject)doesNotRecognizeSelector:] + 187     3 CoreFoundation 0x00d5c966 __ 转发 + 966     4 CoreFoundation 0x00d5c522 _CF_forwarding_prep_0 + 50     5 JobAddSite 0x000039c1 - [ResultViewController next] + 262     6 UIKit 0x002db4fd - [UIApplication sendAction:to:from:forEvent:] + 119     7 UIKit 0x004edcc3 - [UIBarButtonItem(UIInternal)_sendAction:withEvent:] + 156     8 UIKit 0x002db4fd - [UIApplication sendAction:to:from:forEvent:] + 119     9 UIKit 0x0036b799 - [UIControl sendAction:to:forEvent:] + 67     10 UIKit 0x0036dc2b - [UIControl(内部)_sendActionsForEvents:withEvent:] + 527     11 UIKit 0x0036c7d8 - [UIControl touchesEnded:withEvent:] + 458     12 UIKit 0x002ffded - [UIWindow _sendTouchesForEvent:] + 567     13 UIKit 0x002e0c37 - [UIApplication sendEvent:] + 447     14 UIKit 0x002e5f2e _UIApplicationHandleEvent + 7576     15 GraphicsServices 0x01743992 PurpleEventCallback + 1550     16 CoreFoundation 0x00dcc944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52     17 CoreFoundation 0x00d2ccf7 __CFRunLoopDoSource1 + 215     18 CoreFoundation 0x00d29f83 __CFRunLoopRun + 979     19 CoreFoundation 0x00d29840 CFRunLoopRunSpecific + 208     20 CoreFoundation 0x00d29761 CFRunLoopRunInMode + 97     21 GraphicsServices 0x017421c4 GSEventRunModal + 217     22 GraphicsServices 0x01742289 GSEventRun + 115     23 UIKit 0x002e9c93 UIApplicationMain + 1160     24 JobAddSite 0x00002af8 main + 102     25 JobAddSite 0x00002a89 start + 53 ) 抛出'NSException'实例后调用终止

2 个答案:

答案 0 :(得分:1)

UITableView *tv = (UITableView *)self.view;
[tv reloadData];

为什么不简单地使用您创建的* uITableView ivar?我认为这是你的问题。

答案 1 :(得分:0)

这是一个老问题,但万一有人通过谷歌来到这里... 尝试将调用包装到[tv reloadData];在主线程调度中,如:

dispatch_async(dispatch_get_main_queue(), ^{
    [tv reloadData];
});