0

UITableViewを使用して簡単なRSSReaderを作成しました。ViewControllerクラスにUITableDataSourceを実装することで、指定したフィードを表示できます。ただし、テキストボックスに入力されたフィードを表示しようとしています。NSLogsを使用してデバッグしましたが、ボタンを押すと、入力されたフィードが適切に解析されます。ただし、[[self tableOfFeeds] reload]ビューは更新されません。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setNf: [[NewsFeed alloc] init]];
    if ([textFieldUserInput.text length] == 0)
    link = @"http://afeedurl.com/feed.rss";
     [self.nf setFeedUrl:[NSURL URLWithString:link]];
    [self.nf retrieveFromInternet];
    for (id newsItem in [self.nf newsStories]){ // Debug
        NSDictionary * d = (NSDictionary *) newsItem;
        NSLog(@"Title: %@ \n %@", [d objectForKey:@"title"], [d objectForKey:@"description"]);
    }
    [[self tableOfFeeds] setDataSource:self];
    [[self tableOfFeeds] setRowHeight:70.0];
    [[self tableOfFeeds] reloadData];
}

そして、これがフィードを変更するアクションです。

- (IBAction)refreshFeed:(id)sender {
    NSString * ui = self.textFieldUserInput.text;
    if ([ui length] == 0) // If text field is blank, then reload the old feed from internet.
        [[self tableOfFeeds] reloadData];
    else {
        [self.nf setFeedUrl: [NSURL URLWithString:ui]];
        [self.nf retrieveFromInternet];
        [[self tableOfFeeds] setDataSource: self];
        [[self tableOfFeeds] reloadData];

        for (id newsItem in [self.nf newsStories]){ // Debug
            NSDictionary * d = (NSDictionary *) newsItem;
            NSLog(@"Title: %@ \n %@", [d objectForKey:@"title"], [d objectForKey:@"description"]);
        }
    }
}
4

1 に答える 1

0

質問の上記のコードは有効なようです。この方法で問題を追跡しました。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellId = @"feeds";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        NSMutableDictionary * news = (NSMutableDictionary *) [[[self nf] newsStories] objectAtIndex: indexPath.row];
        [[cell textLabel] setText: [news objectForKey:@"title"]];
        [[cell detailTextLabel] setText: [news objectForKey:@"description"]];
        [[cell detailTextLabel] setNumberOfLines:2];

    }
        return cell;
}

NSMutableディクショナリを宣言し、セルテキストを設定する部分は、ifステートメントの外にある必要があります。

変更したいセルのほとんどにすでにデータが含まれているため、これは機能すると思います。したがって、alloc/initを呼び出す必要はありません。

于 2012-10-11T02:57:35.303 に答える