سؤال

I am trying to use refreshControl doing everything by Code the problem appears when I pull the tableView and I call the server. It tells me this error :

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds for empty array'

When I run this simple application I watch the data in the table view. I don't know why I have this problem.

Here is the code:

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * data;
@property (nonatomic, strong) UIRefreshControl *spinner ;
@end

@implementation YPProjectListViewController

@synthesize tableView;
@synthesize data;
@synthesize spinner;

- (void)viewDidLoad {
    [super viewDidLoad];
    spinner = [[UIRefreshControl alloc]initWithFrame:CGRectMake(130, 10, 40, 40)];
    [self loadProjectsFromService];
    [spinner addTarget:self action:@selector(loadProjectsFromService) forControlEvents:UIControlEventValueChanged];
    [tableView addSubview:spinner];


    }



-(void)loadProjectsFromService{
     [spinner beginRefreshing];
    self.data = [[NSMutableArray alloc] init];
    [self.view addSubview:self.tableView];
    __weak typeof(self) weakSelf = self;
    successBlock = ^(NSMutableArray *newData) {
        if ([newData count] > 0) {
            [weakSelf refreshData:newData];
        }

    };
        [spinner endRefreshing];
    [ypNetManager getProjectListWithSuccessBlock:successBlock error:NULL];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Custom getter

- (UITableView *)tableView {
    //custom init of the tableview
    if (!tableView) {
        // regular table view
        tableView = [[UITableView alloc] initWithFrame:UIEdgeInsetsInsetRect(self.view.bounds, tableViewInsets) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        return tableView;
    }
    return tableView;
}

#pragma mark - Private methods 

- (void)refreshData:(NSMutableArray *)newData {
    NSLog(@"data %@", newData);
    self.data = newData;
    [self.tableView reloadData];
}
هل كانت مفيدة؟

المحلول

I don't think there is anything wrong with your fetching data from server. The only thing you might be doing incorrectly is not to refresh the tableView when you reinitialize self.data.

When you pull down and release table view needs to display the 6th tableView cell which went out of viewport and your cell needs 6th object from your data but your data is not present anymore.

just insert the following.

-(void)loadProjectsFromService{
    [spinner beginRefreshing];
    self.data = [[NSMutableArray alloc] init];

    [self.tableView reloadData]; //Insert table reload here.

    //... rest of your code ...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top