UITableViewController没有加载顶部单元格的数据

时间:2016-08-16 04:51:51

标签: objective-c uitableview

我在UITableViewController上显示数据。当它加载视图时,它不显示前几个单元格的数据。当我向下滚动到第3个单元格时,它开始显示数据。我不知道发生了什么。请帮我找到解决方案。

// Method to load data from server.

-(void)searchJobs:(NSInteger)page {

    NSError *error;
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseURLJobs,SearchJobsByCategory]];
    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];

    NSDictionary *params=[[NSDictionary alloc]initWithObjectsAndKeys:@"",@"category",[NSString stringWithFormat:@"%ld",(long)self.currentPage],@"pageNo",nil];

    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [urlRequest setHTTPMethod:@"POST"];

    NSData *postData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
    [urlRequest setHTTPBody:postData];

    dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                    if(error == nil)
                                    {
                                        NSError *jsonError;
                                        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                                                             options:kNilOptions
                                                                                               error:&jsonError];
                                        self.currentPage = [[jsonObject objectForKey:@"number"] integerValue];
                                        self.totalPages  = [[jsonObject objectForKey:@"totalPages"] integerValue];
                                        for(NSDictionary *item in [jsonObject objectForKey:@"content"])
                                        {
                                            searchMapObject = [[SearchMapTableObject alloc] initWithJSONData:item];
                                            [tableDataSource addObject:searchMapObject];
                                        }
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            [self.tableView reloadData];
                                            NSLog(@"reload data");
                                        });
                                    }
                                    else{
                                        [AppDel showAlertWithMessage:error.localizedDescription andTitle:nil];
                                    }
                                }];
    [dataTask resume];
}

 // table view delegates.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"index path : %ld",(long)indexPath.row);
NSUInteger row = [indexPath row];
NSUInteger count = [self.tableDataSource count];

if (row == count) {
    NSLog(@"loading cell");
    cell = [tableView dequeueReusableCellWithIdentifier:@"LoadingCell" ];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"LoadingCell"];
    }
    if(row == 0) {

    } else {
        cell.textLabel.text = @"Load more ...";
        cell.textLabel.font = MONTSERRAT_LIGHT(15.0);
        cell.textLabel.textColor = ENABLED_GREEN_COLOR;
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[cell.contentView viewWithTag:100];
    [activityIndicator startAnimating];

} else {
    NSLog(@"map cell");
    cell = [tableView dequeueReusableCellWithIdentifier:@"MapCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:@"MapCell"];
    }

SearchMapTableObject *object = [tableDataSource objectAtIndex:indexPath.row];
NSLog(@"object :%@", object.title);

UITextView *textView = (UITextView *)[cell.contentView viewWithTag:2];
textView.text = object.title;

return cell; 
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height =  0;
    if(indexPath.row == self.tableDataSource.count) {
        height = 44.0;
    } else {
        if(IS_IPHONE_6P) {
            height = 566;
        } else if(IS_IPHONE_6) {
            height = 505;
        } else if(IS_IPHONE_5) {
            height = 420;
        }
    }
    return height;
}

0 个答案:

没有答案