0

서버에서 데이터를 가져 와서 tableview에 표시 할 수 없습니다 ... jsonarray 변수에 데이터를 저장하고 있습니다 ... 그러나 jsonarray.count을 완료 처리기 외부에 표시합니다. (열 번호 numberOfRowsInSection).테이블 뷰에 데이터를 표시 할 수 없습니다.

@interface Staffdir() 
@property(nonatomic,strong) NSMutableArray* jsonarr; 
@end 

@implementation Staffdir 

- (void)viewDidLoad { 

    _namesarr=[[NSMutableArray alloc]init]; 
    _occupationarr=[[NSMutableArray alloc]init]; 
    _Phonearr=[[NSMutableArray alloc]init]; 
    _faxnumarr=[[NSMutableArray alloc]init]; 

    NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init]; 
    NSURL*url=[NSURL URLWithString:@"http:someurl"]; 
    [req setURL:url]; 


     [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
      self.jsonarr = [NSJSONSerialization JSONObjectWithData:data 
                  options:kNilOptions 
                  error:&error]; 

      NSDictionary *dict=[[NSDictionary alloc]init]; 

      for(int i=0;i<self.jsonarr.count;i++){ 

       dict=[self.jsonarr objectAtIndex:i]; 

       [_namesarr addObject:[dict objectForKey:@"Name"]]; 
       [_faxnumarr addObject:[dict objectForKey:@"Fax_Number"]]; 
       [_Phonearr addObject:[dict objectForKey:@"Phone"]]; 
       [_occupationarr addObject:[dict objectForKey:@"Job_Title"]]; 
      } 
     }] resume]; 


    [self.tableview reloadData]; 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return self.jsonarr.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    Staffcustom *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (cell == nil) { 
     cell = [[Staffcustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 

    cell.namelabel.text=[_namesarr objectAtIndex:indexPath.row]; 
    cell.namelabel.textColor = [UIColor blueColor]; 
    cell.occupationlabel.textColor=[UIColor colorWithRed:0.5 green:0.0 blue:1 alpha:1.0]; 
    cell.occupationlabel.text=[_occupationarr objectAtIndex:indexPath.row]; 
    [email protected]"12345678"; 
    [email protected]"12345678"; 

    UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,0.1)]; 
    additionalSeparator.backgroundColor = [UIColor blueColor]; 

    [cell addSubview:additionalSeparator]; 

    return cell; 
} 

- (IBAction)Btn_Back:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
+0

'dataTaskWithRequest'는 이미 asycn이며, 그것에 대한 디스패치를 ​​호출 할 필요가 없습니다. 완료 핸들러 안에서'[self.tableview reloadData];를 호출해야한다. – Larme

+0

요청을 반환하는 데이터에 값이 있습니까? 그렇다면 json 응답 구조를 알려주십시오. – DiegoQ

+0

이것을 시도하십시오. NSDictionary * response = [[NSDictionary alloc] init]; 응답 = [[NSMutableArray alloc] init]; for (int i = 0; i) [responseDict valueForKey : @ "dictonaryName"] 개수]; i ++) { [응답 응답 객체 : [[responseDict valueForKey : @ "name"] objectAtIndex : i]]; } [self.tableView reloadData]; –

답변

1

이 시도 :

U 아직도 날을 업데이트 오류에 직면하는 경우, 그것을 확인

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 

     if ([response count] > 0) { 
      //creating the status lable 
      UILabel * name = [[UILabel alloc]initWithFrame:CGRectMake(80*XScale, 25*YScale, XScale*200, YScale*15)]; 

      name.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:12.0f]; 
      NSString *time=[[response objectAtIndex:indexPath.row]valueForKey:@“name”]; 
      [cell.contentView addSubview:name]; 

[json PostMethod:method parameters:parameter success:^(NSDictionary *responseDict){ 

     response=[[NSMutableArray alloc]init]; 
     for (int i=0; i < [[responseDict valueForKey:@"transactions"] count]; i++) { 
      [response addObject:[[responseDict valueForKey:@"transactions"]objectAtIndex:i]]; 
     } 

     [self.tableView reloadData]; 

TableviewCell 형식을 서버에서 데이터를에 geting. 나는 도움이 될 것이다. 당신이 쉽게 문제를 해결하는 데 도움이됩니다 통해 UR 응답을 제공합니다.

관련 문제