2011-09-07 106 views
1

我有一个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; 
} 
+0

验证数据源和委托是否已正确设置。 –

+0

在界面生成器数据源和委托连接到文件的所有者。 – PETER

+0

也验证thubnailArray并发布代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath。 –

回答

0

你必须使用以下。

if (count > 0) 
{ 
    [tableview reloadData]; 
    NSLog(@"reload data test"); 

} 
+0

感谢您的回复,“count”实际上是一个传递给“loadJsonData”方法的参数。它实际上并不代表数据源计数。总之,它正在执行该条件,我能够看到“重载数据测试“在控制台中。对于不好的命名约定。 – PETER

0

我不禁觉得你performSelectorOnMainThread:是有点多余,可以会导致您的问题,但我不明白为什么。你不能只使用:[tableview reloadData];

答案提供了我的@iPhoneiPadDev指出我错过了缺陷。改变你如果到if (count >= 0)

+0

我也尝试过[tableview reloadData]也没有重新加载。计数值将是0或1.我想重新加载,如果计数值为1. – PETER

0

修改您的代码如下......

- (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; 
    } 
+0

感谢您的回应。我修改了它...仍然没有工作。在我的代码中,“numberOfRowsInSection”方法没有执行。因此,如果我没有弄错,我认为“reloadData”方法没有得到执行。 – PETER

1

首先检查区段的号码。如果由于某种原因您的代码返回0,则其他委托函数都不会被调用。

+0

Thnx很多老板:) 我已经尝试了一切,但这只适用于我。 –

-1

99.99%,问题只涉及到设置delagatedataSourcetableview(或者,如果你是从厦门国际银行将它们设置你可能没有救了厦门国际银行),如果numberOfRowsInSection没有被调用。

删除所有代表。重新分配代表并确保您保存包括xib在内的所有文件。