如果tableView numberOfRowsInSection == 0,则显示警报

时间:2013-03-05 06:31:24

标签: ios objective-c

如果tableView中没有结果,我想显示警告。我使用如下所示的numberOfRowsInSection,但警报未显示。我还删除了if语句来检查计数,以防它出现问题。有谁知道为什么没有显示警报?任何帮助都会很棒。谢谢!

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];

    }    
    }
}

3 个答案:

答案 0 :(得分:2)

它没有显示,因为您在显示警告之前返回:

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

答案 1 :(得分: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]; 
}

答案 2 :(得分:0)

检查条件后写入return语句。

   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];