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

问题描述:

我在UITableViewController上显示数据。当它加载视图时,它不显示第一对单元格的数据。当我向下滚动到第3个单元格时,它开始显示数据。我不知道发生了什么。请帮我找到解决办法。UITableViewController没有加载顶层单元格的数据

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

为什么电池=零;在cellForRowAtIndexPath方法。 – pkc456

+0

我评论cell = nil。它没有区别。 – Rains

+0

你在'cellForRowAtIndexPath:'中调用'dequeueReusableCellWithIdentifier:'在哪里?你在哪里给'cell“赋值? – rmaddy

我已经犯了个小错误 我发现,更改以下

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

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

现在工作正常的代码..