2013-03-05 39 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]; 
相关问题