0

このチュートリアルを使用して、iPhone ブログ アプリを作成しました。作成された配列のカウントを使用して、行数を設定します。現在 200 を超えて増え続けているため、すべての項目を実行したくはありません。セクション内の行に任意の数を返すと、空の配列の境界を超えて約 0 というエラーで常にクラッシュします。最初の 20 項目のみをテーブルビューに表示できるようにするには、このチュートリアルから他に何を調整する必要がありますか?

更新:私はいくつかの進歩を遂げていると思います。配列をソートする reqeustFinished メソッドで、次のように編集しました。

NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];                
            [entries removeLastObject];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {
                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntry *entry1 = (RSSEntry *) a;
                        RSSEntry *entry2 = (RSSEntry *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];
                    NSLog(@"%@", entries);

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

エントリのremoveLastObjectに行を追加すると、最初のアイテムが削除されました(これは理想的には、初日に必要な唯一のものです)。Index でオブジェクトの範囲を削除できる他の方法はありますか?

テーブル ビュー クラスとデータソースのコードは次のとおりです。

- (void)refresh {
    self.allEntries = [NSMutableArray array];
    self.queue = [[[NSOperationQueue alloc] init] autorelease];
    self.feeds = [NSArray arrayWithObjects:@"addressofxmlfile",
                  nil];
    for (NSString *feed in _feeds) {
        NSURL *url = [NSURL URLWithString:feed];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [_queue addOperation:request];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];    
    self.refreshControl = [[UIRefreshControl alloc] init];

    [self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged];
    [self refresh];
}
-(void) refreshInvoked:(id)sender forState:(UIControlState)state {
    // Refresh table here...
    [_allEntries removeAllObjects];
    [self.tableView reloadData];
    [self refresh];
}

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {            

        NSString *blogTitle = [channel valueForChild:@"title"];                    

        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {

            NSString *articleTitle = [item valueForChild:@"title"];
            NSString *articleUrl = [item valueForChild:@"guid"];            
            NSString *articleDateString = [item valueForChild:@"pubdate"];        
            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
            NSString *articleImage = [item valueForChild:@"description"];
            NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
            NSString *dateofarticle = [dateFormatter stringFromDate:articleDate];
            NSString *days = [articleImage substringFromIndex:7];


            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
                                                      articleTitle:articleTitle
                                                        articleUrl:articleUrl
                                                       articleDate:articleDate
                                                      articleImage:articleImage
                                                              date:thedate] autorelease];
            [entries addObject:entry];

        }      
    }

}


- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
        [self parseRss:rootElement entries:entries];
    }else {
        NSLog(@"Unsupported root element: %@", rootElement.name);
    }    
}

- (void)requestFinished:(ASIHTTPRequest *)request {

    [_queue addOperationWithBlock:^{

        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] 
                                                               options:0 error:&error];
        if (doc == nil) { 
            NSLog(@"Failed to parse %@", request.url);
        } else {

            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];                

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {

                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntry *entry1 = (RSSEntry *) a;
                        RSSEntry *entry2 = (RSSEntry *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                }                            

            }];

        }        
    }];
    [self.refreshControl endRefreshing];

}

- (void)requestFailed:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    NSLog(@"Error: %@", error);
    [self refresh];
}

#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  // [tableView setBackgroundColor:[UIColor redColor]];
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     
    return [_allEntries count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";  

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {  
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }



    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];


           CALayer * l = [cell.imageView layer];
        [l setMasksToBounds:YES];
        [l setCornerRadius:11];
        [l setBorderWidth:2.0];
        [l setBorderColor:[[UIColor blackColor] CGColor]];
        NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [dateFormatter setDateStyle:NSDateFormatterShortStyle];
        NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
        UIFont *cellFont = [UIFont fontWithName:@"Papyrus" size:19];
        UIFont *cellFont2 = [UIFont fontWithName:@"Papyrus" size:17];
       // cell.imageView.image = [UIImage imageNamed:@"icon@2x.png"];
        cell.textLabel.text = entry.date;
        cell.detailTextLabel.text = entry.articleTitle;
    cell.detailTextLabel.textColor = [UIColor blackColor];

        cell.textLabel.font = cellFont;
        cell.detailTextLabel.font = cellFont2;





    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 73.4;


}
4

1 に答える 1

0

ここで、Refresh メソッドで allEntries を新しい配列に割り当てています。

- (void)refresh {
    self.allEntries = [NSMutableArray array];

このチュートリアルでは、ViewDidLoad メソッドでのみこれを行います。リフレッシュがいつ/どのように呼び出されるか正確にわからないため、これが問題かどうかはわかりません。

ここでセル/行を非表示にする方法を示すこの回答が見つかりました。

于 2012-12-17T21:06:26.857 に答える