2017-10-16 92 views
0

我試圖找到一個解決方案,但到目前爲止沒有運氣,我可以在tableview中顯示一個自定義標籤,其中numberOfSections返回零,如果數組中沒有數據其他我要返回所需的。當數據發現或沒有時,在tableview中自定義標籤文本?目標c

現在我發送一個viewDidLoad Json請求,所以我可以在tableview中填充數據。當視圖加載時,最初我得到沒有數據在自定義標籤中找到這很好,因爲初始數組是空的,並且當我發送JSO請求時將文本更改爲「獲取數據請等待」,但是沒有任何影響而且我經常顯示沒有找到數據。

你們能請大家指教我錯過了什麼嗎?

請不要張貼numberOfRows的解決方案作爲1和部分返回作爲1

在此先感謝。

+0

llyas你檢查填充表前陣算什麼?你正在重新加載主線程上的表嗎?有許多事情可以檢查和解決,使用斷點來調試。 –

+0

你可以上傳一些代碼更清晰 –

+0

Tushar,是的,我已經檢查了你上面寫的東西,一切都如預期。 –

回答

0

得到Json數組後檢查這個條件。

if([YourJsonArray count]==0) 
{ 
    [self showalert:@"" msg:@"No data found"];  
} 
else 
{ 
    [self.tbleView reloadData]; 
} 

警報方法

-(void)showalert:(NSString *)strTitle msg :(NSString *)message 
{ 
UIAlertController * alert=[UIAlertController alertControllerWithTitle:strTitle 
                   message:message 
                 preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Ok " 
                style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action) 
          { 

          }]; 

[alert addAction:yesButton]; 
[self presentViewController:alert animated:YES completion:nil]; 

} 

而且你可以在tableview中的numberofrows或部分按您的要求

+0

lalit,我不想隱藏一個標籤,我只是想在請求發送到「提取數據請稍候」時更改該標籤的文本,如果沒有找到記錄,我想將文本更改爲「找不到數據」。通過您的解決方案,我隱藏並取消隱藏不需要的標籤。 –

+0

你可以使用這個檢查我的更新答案的警報 –

0

下面,供大家參考代碼中使用YourJsonArray。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    NSInteger numOfSections = 0; 
    if ([searchArray count]) 
    { 
     self.tableView = UITableViewCellSeparatorStyleSingleLine; 
     numOfSections    = 1; 
     self.tableView.backgroundView = nil; 
    } 
    else{ 

    if ([results count]) 
    { 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     numOfSections = 1; 
     self.tableView.backgroundView = nil; 
    } 
    else 
    { 
     self.noDataLabel.text    = @"No Data Available"; 
     self.noDataLabel.textColor  = [UIColor blackColor]; 
     self.noDataLabel.textAlignment = NSTextAlignmentCenter; 
     self.tableView.backgroundView = self.noDataLabel; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    } 
    } 
    return numOfSections; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSUInteger rows =0; 
    if(self.searchBar.text.length > 0){ 
     rows = [searchArray count]; 
     return rows; 
    } 
    else{ 

    rows = results.count; 

    } 
    DLog(@"%ld",rows); 
    return rows; 
} 

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

    if((self.searchBar.text.length > 0)){ 

     sObject * searchObj = [searchArray objectAtIndex:indexPath.section] ; 
     [cell setData:searchObj]; 
    } 
    else if([results count] == 0){ 
     return cell; 
    } 
    else { 

    sObject * ordObj = [results objectAtIndex:indexPath.row]; 

    [cell setData:ordObj]; 
    } 
    return cell; 
} 

,我添加以下線路發送Json請求之前

self.noDataLabel.text = @".. Fetching Data Please Wait ..";