如何在表格视图中重新加载数据

Posted

技术标签:

【中文标题】如何在表格视图中重新加载数据【英文标题】:How do i reload data in a tableview 【发布时间】:2012-12-11 12:54:52 【问题描述】:

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

#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 异常预处理 + 185 1 libobjc.A.dylib 0x00f3f313 objc_exception_throw + 44 2 核心基础 0x00ded0bb -[NSObject(NSObject)不识别选择器:] + 187 3 CoreFoundation 0x00d5c966 __转发 + 966 4 核心基础 0x00d5c522 _CF_forwarding_prep_0 + 50 5 JobAddSite 0x000039c1 -[ResultViewController 下一个] + 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 发送事件:] + 447 14 UIKit 0x002e5f2e _UIApplicationHandleEvent + 7576 15 图形服务 0x01743992 PurpleEventCallback + 1550 16 核心基础 0x00dcc944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 17 核心基础 0x00d2ccf7 __CFRunLoopDoSource1 + 215 18 核心基础 0x00d29f83 __CFRunLoopRun + 979 19 核心基础 0x00d29840 CFRunLoopRunSpecific + 208 20 核心基础 0x00d29761 CFRunLoopRunInMode + 97 21 图形服务 0x017421c4 GSEventRunModal + 217 22 图形服务 0x01742289 GSEventRun + 115 23 UIKit 0x002e9c93 UIApplicationMain + 1160 24 JobAddSite 0x00002af8 主要 + 102 25 JobAddSite 0x00002a89 开始 + 53 ) 在抛出 'NSException' 的实例后调用终止

【问题讨论】:

崩溃的原因是什么? 可以添加崩溃错误吗?如果你添加一个异常断点,它会告诉你它在哪里崩溃。 我刚刚添加并编辑了错误的问题 UITableView *tv = (UITableView *)self.view; [tv reloadData]; 根据这一行和异常,您的视图似乎不是UITableView 类的实例。您可以在代码中插入以下行来检查它:NSLog(@"%@", [tv class]);,您将看到您正在处理的内容。 哦,我在崩溃日志中看到,您的 self.view 只是简单的 UIView 类,因此您的视图层次结构肯定有问题。为什么你认为self.viewUITableView 【参考方案1】:
UITableView *tv = (UITableView *)self.view;
[tv reloadData];

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

【讨论】:

【参考方案2】:

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

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

【讨论】:

以上是关于如何在表格视图中重新加载数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iPhone 应用程序中重新加载表格视图

有没有办法在重新加载表格视图数据之前添加加载视图

使用 Swift 在表格视图中重新加载单元格数据

如何在uitableview中加载数据而不每次都重新加载

如何重新加载表格视图? Xcode 11

在我的表格视图中,我没有只有单元格的部分,所以如何在 ios 目标 c 中重新加载表格视图?