2013-03-05 36 views
-1

顯示警報如果在tableView中沒有結果,我想顯示警報。我使用下面的numberOfRowsInSection,但不顯示警報。我還刪除了if語句,以檢查計數是否有錯誤。有誰知道爲什麼警報沒有顯示?任何幫助都會很棒。謝謝!如果tableView numberOfRowsInSection == 0

if ([self.listItems count] == 0) 




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

if (tableView == self.searchDisplayController.searchResultsTableView) { 
    return [self.filteredListItems count]; 
} 

else { 
    return [self.listItems count]; 
    if ([self.listItems count] == 0) { 

    //CALL ALERT HERE  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results" message:@"No 
    results were found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 

    }  
    } 
} 

回答

2

它不顯示,因爲您顯示警報之前返回:

else { 
    return [self.listItems count]; 
... 
+0

謝謝!這現在可以工作,但警報一遍又一遍地顯示。是否有一個簡單的解決辦法,或者我需要創建一種布爾只顯示一次? – Brandon 2013-03-05 06:36:29

+0

更好的做法是不要在此方法中顯示警報,因爲它會多次調用。如果您需要警告用戶無需顯示任何內容,則可以在準備數據源時檢查是否有任何項目,如果沒有,則顯示警報。 – graver 2013-03-05 06:42:06

0

看到的問題是此行return [self.listItems count]; 原因是你的執行不會超越這一點。將其更改爲:

else 
{ 
    if ([self.listItems count] == 0) 
    { 
     //CALL ALERT HERE  
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results" message:@"No 
     results were found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
     return; 
    } 
    return [self.listItems count]; 
} 
0

在檢查條件後檢查返回語句。

if ([self.listItems count] == 0) { 

    //CALL ALERT HERE  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results" message:@"No 
    results were found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 

    } 
return [self.listItems count]; 
相關問題