Friday 6 September 2013

Refreshing tableView with refreshcontrol by code Error

Refreshing tableView with refreshcontrol by code Error

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];
}

No comments:

Post a Comment