1

我试图在我的 RSS 提要中实现下拉刷新。但是现在我遇到了一个问题,刚触发它,它就崩溃了!我很确定我在这里遇到了和这个人一样的问题:

超出空数组崩溃范围的索引“5”

我试图解决这个问题,但不知何故我无法这样做。这是我的代码:

#pragma mark -
#pragma mark View lifecycle

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Artikel", @"Artikel");
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if (_refreshHeaderView == nil) {

        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
        view.delegate = self;
        [self.tableView addSubview:view];
        _refreshHeaderView = view;
        [view release];

    }
    [_refreshHeaderView refreshLastUpdatedDate];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
        UIImage *navBarImage = [UIImage imageNamed:@"navbar_ipad.png"];
        [self.navigationController.navigationBar setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];
    } else {

    }

    UIImage *image = [[UIImage imageNamed:@"refresh.png"]retain];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage: image forState:UIControlStateNormal];

    button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.showsTouchWhenHighlighted = YES;

    [button addTarget:self action:@selector(refresh)    forControlEvents:UIControlEventTouchUpInside];

    UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 28, image.size.height) ];

    [v addSubview:button];

    UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];

    self.navigationItem.rightBarButtonItem = forward;

    [forward release];
    [v release];
    [image release];

    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(handleXMLDownloadComplete:) name:EMRXMLDownloadCompleteNotification object:nil];

    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}

-(void)refresh {
    [self.articles awakeFromNib];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(handleXMLDownloadComplete:) name:EMRXMLDownloadCompleteNotification object:nil];
}

- (void)handleXMLDownloadComplete: (NSNotification *)note {
    [self.tableView reloadData];
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    self.thirdViewController.detailItem = [self.articles articleAtIndex:0];

}

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
    //  should be calling your tableviews data source model to reload
    //  put here just for demo
    _reloading = YES;

}

- (void)doneLoadingTableViewData{
    //  model should call this when its done loading
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    //NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    //[[tableView delegate] tableView:tableView didSelectRowAtIndexPath:indexPath];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [articles count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Configure the cell.
    NSMutableDictionary *article = [self.articles articleAtIndex:indexPath.row];
    NSString *title = [article objectForKey:@"title"];
    cell.textLabel.text = title;

    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.thirdViewController.detailItem = [self.articles articleAtIndex:indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[self thirdViewController]checkHistory];
}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 

    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];

}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{

    [self refresh];
    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];

}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{

    return _reloading; // should return if data source model is reloading

}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{

    return [NSDate date]; // should return date data source was last changed

}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    self.thirdViewController = nil;

    [super viewDidUnload];
}

- (void)dealloc {
    [articles release], articles = nil;
    [thirdViewController release];

    [super dealloc];
}

@end

错误代码:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 17 beyond bounds for empty array'
*** First throw call stack:
(0x1748022 0x196ecd6 0x1734d88 0x20944 0x1d29b 0x712c54 0x7133ce 0x6fecbd 0x6fd6c6 0x70d92b 0x70decf 0x6bd956 0x70e74e 0x6d701e 0x6bdad7 0x70e7c5 0xf390 0x1d4bb 0x6c6ca3 0x6c830e 0x93fe29 0x93f133 0x9403bf 0x942a21 0x94297c 0x93b3d7 0x6a01a2 0x6a0532 0x686dc4 0x67a634 0x22eeef5 0x171c195 0x1680ff2 0x167f8da 0x167ed84 0x167ec9b 0x22ed7d8 0x22ed88a 0x678626 0x1e3d 0x1db5)
terminate called throwing an exception(lldb)
4

1 回答 1

0

当调用函数egoRefreshScrollViewDataSourceDidFinishedLoading -> scrollView * setContentInset * 时调用cellForRowAtIndexPath函数:所以在我的cellForRowAtIndexPath内部我测试了indexPath.row 是否 >= [myArray count]然后我直接返回了单元格

于 2014-04-09T22:25:50.970 回答