2017-06-15 27 views
0

時,在tableview中顯示爲無數據我是iOS開發新手。我遇到了一個問題。在下面的代碼中,我使用api來獲取數據。 問題:數據沒有在UITableView中顯示。 這是我的代碼當在json數組中存儲的值使用objective-c

- (MXSegmentedPager *)segmentedPager { 
    if (!_segmentedPager) { 

     // Set a segmented pager below the cover 
     _segmentedPager = [[MXSegmentedPager alloc] init]; 
     _segmentedPager.delegate = self; 
     _segmentedPager.dataSource = self; 

     self.tableView1.alwaysBounceVertical = NO; 
    } 
    return _segmentedPager; 
} 
    - (UITableView *)tableView { 
    if (!_tableView) { 
     //Add a table page 
     _tableView = [[UITableView alloc] init]; 
     _tableView.delegate = self; 
     _tableView.dataSource = self; 
     self.tableView.separatorColor = [UIColor clearColor]; 
     self.tableView.alwaysBounceVertical = NO; 
    } 
    return _tableView 
    ; 
} 
- (UITableView *)tableView2 { 
    if (!_tableView2) { 
     //Add a table page 
     _tableView2 = [[UITableView alloc] init]; 
     _tableView2.delegate = self; 
     _tableView2.dataSource = self; 

     self.tableView2.alwaysBounceVertical = NO; 
     _tableView2.tableFooterView=[UIView new]; 
    } 
    return _tableView2 
    ; 
} 

#pragma mark <MXSegmentedPagerDelegate> 

- (CGFloat)heightForSegmentedControlInSegmentedPager:(MXSegmentedPager *)segmentedPager { 
    return 30.f; 
} 

- (void)segmentedPager:(MXSegmentedPager *)segmentedPager didSelectViewWithTitle:(NSString *)title { 
    // NSLog(@"%@ page selected.", title); 
} 

- (void)segmentedPager:(MXSegmentedPager *)segmentedPager didScrollWithParallaxHeader:(MXParallaxHeader *)parallaxHeader { 
    // NSLog(@"progress %f", parallaxHeader.progress); 
} 

#pragma mark <MXSegmentedPagerDataSource> 

- (NSInteger)numberOfPagesInSegmentedPager:(MXSegmentedPager *)segmentedPager { 
    return 2; 
} 

- (NSString *)segmentedPager:(MXSegmentedPager *)segmentedPager titleForSectionAtIndex:(NSInteger)index { 
    return @[@"Profile", @"Room Details"][index]; 
} 

- (UIView *)segmentedPager:(MXSegmentedPager *)segmentedPager viewForPageAtIndex:(NSInteger)index { 
    // return NO; 
    return @[self.tableView, self.tableView1][index]; 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    if (tableView==_tableView) { 
     return 1; 
    } 


    else if (tableView==_tableView1) { 
     return [[Dict objectForKey:@"room_details"]count]; 
    } 
else 
     return 1; 

} 
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView==_tableView) { 
     return 7; 
    } 
    else if(tableView==_tableView1) 
     { 
     return [[Dict objectForKey:@"accom_profile"]count];  
     } 
else 
     return NO; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView==_tableView) { 
     static NSString *CellIdentifier = @"cel"; 
     cell1=[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell1==nil) { 
      NSArray*toplevelobject=[[NSBundle mainBundle] loadNibNamed:@"prof_cel1" owner:self options:nil]; 
      cell1=[toplevelobject objectAtIndex:0]; 
     } 
       if (indexPath.row==0) { 

      NSString *s1=[[[Dict objectForKey:@"accom_profile"] objectAtIndex:0] objectForKey:@"acc_building_no"]; 
      [email protected]"Buliding No"; 
      if (s1==(id)[NSNull null] || [s1 isEqualToString:@""]) 
      { 
       [email protected]"-"; 
      } 
      else{ 
       cell1.sub_labl.text=s1; 
      } 
     } 

     return cell1; 
    } 

    else if (tableView==_tableView1) 
    { 
     static NSString *CellIdentifier = @"cel"; 

     room=[self.tableView1 dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (room==nil) { 
      NSArray*toplevelobject=[[NSBundle mainBundle] loadNibNamed:@"room_details" owner:self options:nil]; 
      room=[toplevelobject objectAtIndex:0]; 
     } 
     NSString *s1=[[[Dict objectForKey:@"room_details"]objectAtIndex:indexPath.section]objectForKey:@"intake_capacity"]; 
     if (s1==(id)[NSNull null] || [s1 isEqualToString:@""]) { 
      [email protected]"-"; 
     } 
     else { 
      room.intake.text=s1; 
     } 
     return room; 
    } 
else 
     return nil; 
} 

因此我得到了輸出。我的api格式如下。

"accom_profile" =  (
       { 
      "acc_address" = ""; 
      "acc_building_no" = 1; } 
    ); 
"room_image" =  ({ 
      "file_path" = "http:example.com"; 
    ); 

}) 

但是,當API是這樣,

"accom_profile" =  (
       { 
      "acc_address" = ""; 
      "acc_building_no" = 1; } 
    ); 
"room_image" =  (
    ); 

在tableview2詞典中的值具有空值。那麼當我在index1選擇segmentedpager時如何在tableview中顯示沒有數據?

+0

只是想知道你爲什麼需要2實現代碼如下?它可以完成使用分組的tableview而不是分節?我只是覺得維護起來會更容易 – Joshua

+0

我需要顯示更多的數據,所以我使用了更多的表格視圖。當上面提到 – jesti

+0

中提到的房間圖像中有空值時,如何顯示爲無數據,如果有人知道答案,請回復。 – jesti

回答

0
"accom_profile" =  (
       { 
      "acc_address" = ""; 
      "acc_building_no" = 1; } 
    ); 
"room_image" =  (
    ); 

這是一個JSON數據,我假設你在字典中放置這樣說NSDictionary * dictionary

關鍵room_image包含NSArray。取它並把它放在一個數組說

NSArray * arrayRoomImage = [dictionary valueForKey:@"room_image"]; 

UITableView DataSource方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
if(arrayRoomImage.count!=0) // Checking is array is Not empty 
return arrayRoomImage.count; 

return 1; // executes only if array empty 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
if(arrayRoomImage.count!=0){ // Checking is array is Not empty 
// return the cell when room_image is not empty 
} 
// Code here will execute only if arrayRoomImage is empty 
// return another cell which has a label with text No Data. 
} 
+0

現在,我給了:arrayRoomImage = [Dict objectForKey:@「warranty_card」]; (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {if(tableView == _ tableView9){return [[Dict objectForKey:@「warranty_card」] count];} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)部分{if(tableView == _ tableView2)if(arrayRoomImage.count!= 0) return arrayRoomImage.count; else return arrayRoomImage.count;}但沒有顯示nodata – jesti

+0

@jesti我可以看到你是來自高知...我不明白你的代碼,因爲你有很多tableviews ?? ..我相信你不明白如何UITableVIew工作..我可以幫助你,如果你真的可以解釋你在做什麼? – 2017-06-15 10:41:46