2016-08-16 3 views
0

Я показываю данные на 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

Где вызов 'dequeueReusableCellWithIdentifier:' в вашей 'cellForRowAtIndexPath:'? Где вы назначаете значение 'cell'? – rmaddy

ответ

0

Я сделал небольшую ошибку я обнаружил, что, чтобы изменить код ниже

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

в

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

Сейчас она работает нормально ..

Смежные вопросы