我有一个tableview
第一次正确加载,但它应该在我调用方法loadJsonData
时重新加载数据。在loadJsonData
方法的末尾调用reloadData
方法。但tableview
不重新加载。在调用reloadData
方法之前更新数据源。 tableview
的代表和dataSource也设置为self。UITableview不能重新加载
我检查它是否正在执行numberOfRowsInSection
方法,并发现它没有执行该方法。有谁能够帮助我?
下面是代码:
-(void)loadJsonData:(NSString *)fileName:(int)count
{
titleArray=[[NSMutableArray alloc]init];
dateArray=[[NSMutableArray alloc]init];
descriptionArray=[[NSMutableArray alloc]init];
urlArray=[[NSMutableArray alloc]init];
thumbnailArray=[[NSMutableArray alloc]init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJSON *Parser = [[SBJSON alloc] init];
NSDictionary *data = (NSDictionary *) [Parser objectWithString:fileContent error:nil];
NSArray *items=[data objectForKey:fileName];
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [items sortedArrayUsingDescriptors:sortDescriptors];
items=sortedArray;
for (NSDictionary *item in items)
{
NSString *temp=[item objectForKey:@"title"];
NSString* str = [temp stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[titleArray addObject:str];
temp=[item objectForKey:@"date"];
[dateArray addObject:temp];
temp=[item objectForKey:@"thumbnailURL"];
str = [temp stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[thumbnailArray addObject:str];
temp=[item objectForKey:@"description"];
str = [temp stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[descriptionArray addObject:str];
temp=[item objectForKey:@"htmlURL"];
str = [temp stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[urlArray addObject:str];
}
[Parser release];
if (count==1)
{
[tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
NSLog(@"reload data test");
}
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 1;
}
-(NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"nof rows test..");
return [thumbnailArray count];
}
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell;
cell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];
cell.contentMode=UIViewContentModeScaleAspectFit;
cell.Title.text=[titleArray objectAtIndex:indexPath.row];
cell.description.text=[descriptionArray objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[thumbnailArray objectAtIndex:indexPath.row]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.Image.image=img;
cell.Date.text=[dateArray objectAtIndex:indexPath.row];
NSLog(@"date is : %@",cell.Date.text);
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
return cell;
}
验证数据源和委托是否已正确设置。 –
在界面生成器数据源和委托连接到文件的所有者。 – PETER
也验证thubnailArray并发布代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath。 –