'索引17超出了空数组'TableView的边界 [英] 'index 17 beyond bounds for empty array' TableView

查看:69
本文介绍了'索引17超出了空数组'TableView的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的RSS Feed中实现pull-to-refresh。但是现在我遇到了一个问题,就在我触发它之后,它崩溃了!我很确定我在这里遇到了同样的问题:



索引'5'超出空阵列崩溃的界限



我试过解决问题,但我不能这样做。这是我的代码:

  #pragma mark  -  
#pragma mark查看生命周期

- (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);
}
返回自我;
}

- (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;
[查看发布];

}
[_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;

[转发];
[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);
}

- (无效)刷新{
[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];

}

//确保视图控制器支持旋转,因此拆分视图可以纵向和横向显示。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

#pragma mark -
#pragma mark数据源加载/重新加载方法

- (void)reloadTableViewDataSource {
//应该调用你的tableviews数据源模型来重新加载$ ​​b $ b //这里只是为了演示
_reloading = YES;

}

- (void)doneLoadingTableViewData {
//模型应该在完成加载时调用它
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
// NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// [[tableView委托] tableView:tableView didSelectRowAtIndexPath:indexPath];
}

#pragma mark -
#pragma mark表视图数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
//返回部分的数量。
返回1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
//返回节中的行数。
返回[文章计数];
}

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

//出列或创建适当类型的单元格。
UITableViewCell * cell = [_ tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}

//配置单元格。
NSMutableDictionary * article = [self.articles articleAtIndex:indexPath.row];
NSString * title = [article objectForKey:@title];
cell.textLabel.text = title;

返回单元格;
}

#pragma mark -
#pragma mark表视图委托

- (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方法

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

[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];

}

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

[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];

}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate方法

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView *)查看{

[自我更新];
[self performSelector:@selector(doneLoadingTableViewData)withObject:nil afterDelay:3.0];

}

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

return _reloading; //如果数据源模型正在重新加载,则应返回

}

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

return [ NSDate日期]; //应该返回日期数据源上次更改

}

#pragma mark -
#pragma mark内存管理

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

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

[super viewDidUnload];
}

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

[super dealloc];
}

@end

错误代码:

 由于未捕获的异常'NSRangeException'而终止应用程序,原因:'***  -  [__ NSArrayM objectAtIndex:]:索引17超出空限阵 
***第一掷调用堆栈:
(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)
终止称为抛出异常(lldb)


egoRefreshScrollViewDataSourceDidFinishedLoading - > scrollView * setContentInset <时,调用 cellForRowAtIndexPath 函数/ em> *叫:所以里面我的 cellForRowAtIndexPath 我测试了 indexPath.row是> = [myArray count] 然后我直接返回了单元格


I tried to implement pull-to-refresh into my RSS feed. But now I'm facing a problem, just after I trigger it, it crashes! I'm pretty sure that I've got the same problem like this guy here:

Index '5' beyond bounds of empty array crash

I tried solve the problem but I somehow am not able to do so. Here's my code:

#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

The errorcode:

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)

解决方案

The cellForRowAtIndexPath function called when the function egoRefreshScrollViewDataSourceDidFinishedLoading -> scrollView *setContentInset* called: so inside my cellForRowAtIndexPath I tested if the indexPath.row is >= [myArray count] then I returned the cell directly

这篇关于'索引17超出了空数组'TableView的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆