0

パーサーを備えた RSS フィードを持つ Web サイト用のアプリを開発しています。新しい投稿を探すために更新ボタンを作成しましたが、機能します。しかし今、新しい投稿が見つからない場合は、「投稿が見つかりません」という 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

呼び出しは、接続を開始した直後ではなく、 でreloadData行う必要があります (これは非同期ネットワーク呼び出しであり、何かがフェッチされる前にテーブルがリロードされます)。connectionDidFinishLoadingアラートもそこで開始する必要があります。すべての接続デリゲートを実装する必要があります。うまくいけば、それは完了しています。そうでない場合、ここに基本的な例があります。

于 2012-08-17T19:39:41.577 に答える