如果未找到新帖子,则显示UIAlertView

时间:2012-08-17 19:34:22

标签: objective-c uitableview rss uialertview nsxmlparser

我正在为我的网站开发一个带有解析器的RSS Feed的应用程序。我做了一个刷新按钮来寻找新的帖子,它的工作原理。但现在,我希望它显示一个UIAlertView,如果找不到新帖子,则说“找不到帖子”。

这是我的刷新按钮

- (IBAction)refreshButton:(UIBarButtonItem *)sender
{

    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc] init];

    // Construct a URL that will ask the service for what you want -
    // Note we can concatenate literal strings together on multiple lines in this way it
    // results in a single NSString instance
    NSURL *url = [NSURL URLWithString:
                  @"http://sephardijews.com/feed/"];

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Creating a connecting that will exchange this request for the data from the URL we specifed
    connection = [[NSURLConnection alloc] initWithRequest:req
                                                 delegate:self
                                         startImmediately:YES];

    [[self tableView] reloadData];
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);

}

我怎么能这样做?有if语句的东西吗?

刷新按钮:

- (IBAction)refreshButton:(UIBarButtonItem *)sender
{

    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc] init];

    // Construct a URL that will ask the service for what you want -
    // Note we can concatenate literal strings together on multiple lines in this way it
    // results in a single NSString instance
    NSURL *url = [NSURL URLWithString:
                  @"http://sephardijews.com/feed/"];

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Creating a connecting that will exchange this request for the data from the URL we specifed
    connection = [[NSURLConnection alloc] initWithRequest:req
                                                 delegate:self
                                         startImmediately:YES];

    [[self tableView] reloadData];
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);

    if ([[self tableView] numberOfRowsInSection:0] > someNumberVariableForLastCount) {
        someNumberVariableForLastCount = [[self tableView] numberOfRowsInSection:0];
        [[self tableView] reloadData];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No New Posts" message:@"There were no new posts found" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [alert show];
    }
}

2 个答案:

答案 0 :(得分:0)

您的reloadData呼叫应该在connectionDidFinishLoading中进行,而不是在您开始连接之后(它是异步网络呼叫,在获取任何内容之前将重新加载表)。警报也应该在那里开始。你需要实现所有连接委托的东西,希望你已经完成了。如果没有,基本示例here

答案 1 :(得分:0)

这会将您指定的部分中的当前行数与最后一次计数的变量进行比较:

  if ([[self tableView] numberOfRowsInSection:0] > someNumberVariableForLastCount) {
            someNumberVariableForLastCount = [[self tableView] numberOfRowsInSection:0];
            [[self tableView] reloadData];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"no new items" message:@"OH NO!!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
            [alert show];
        }