开发者

UITableview not reloading

I have a tableview loading properly for the first time, but it should reload the data when I'm calling the method loadJsonData. The reloadData method is called at the end of the loadJsonData method. But the tableview is not reloading. The data source is updated before calling the reloadData method. The delegate and dataSource of tableview are also set to self.

I checked if it is executing numberOfRowsInSection method and found that it is not executing the method. Can anybody help me?

Below is the code:

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

EDIT

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


Check the number of section first. If for some reason your code returns 0, none of the other delegate functions will be called.


I can't help but feel that your performSelectorOnMainThread: is a little redundant and may be causing your problem, though I can't see why. Could you not just use: [tableview reloadData];

The answer provided my @iPhoneiPadDev points out the flaw I missed. Change your if to if (count >= 0)


You have to use following.

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

}


Modify your code as given below...

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


99.99% the problem is only related to settings delagate and dataSource for tableview (or you might not have saved the xib if you are setting them from xib) if the numberOfRowsInSection is not being Invoked.

remove all delegates. reassign delegates and ensure you are saving all your files including xib.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜