0

我正在为我的网站开发一个应用程序,该应用程序具有带有解析器的 RSS 提要。我做了一个刷新按钮来查找新帖子,它可以工作。但是现在,如果没有找到新帖子,我希望它显示一个 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];
    }
}
4

2 回答 2

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];
        }
于 2012-08-17T19:44:37.360 回答
0

Your reloadData call should occur in connectionDidFinishLoading, not right after you start the connection (it is an asynchronous network call, the table will be reloaded before anything is even fetched). The alert should be kicked off there too. You need to implement all the connection delegate stuff, hopefully you've done that. If not, basic example here.

于 2012-08-17T19:39:41.577 回答