简体   繁体   English

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

[英]UITableViewController is not loading data for top cells

I am displaying data on UITableViewController. 我在UITableViewController上显示数据。 When It loads view, Its not displaying data for first couple of cells. 当它加载视图时,它不显示前几个单元格的数据。 When I scroll down upto 3rd cell, It starts displaying data. 当我向下滚动到第三个单元格时,它开始显示数据。 I have no idea what is happening. 我不知道发生了什么事。 Please help me to find solution. 请帮助我找到解决方案。

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

I have made a small mistake I found that to change the code below 我犯了一个小错误,发现要更改以下代码

 cell = [tableView dequeueReusableCellWithIdentifier:@"MapCell"];

to

 cell = [tableView dequeueReusableCellWithIdentifier:@"MapCell" forIndexPath:indexPath];

Now it is working fine.. 现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM