Lady Chocobo Lady Chocobo - 2 months ago 20
Objective-C Question

Pull to refresh resulting app to crash: NSRangeException, reason: index 8 beyond bounds [0 .. 4]

Sorry if this has been asked before but I can't find the answer to my problem.

I wrote code for pull to refresh but it crashed IF the result array count is different from the

indexPath.row
and I've tried to remove my array objects yet it's still crash.

my code goes for this:

-(void)loadMore:(id) sender {
loadFlag = 1;
NSString *employeeID = @"aaa";
days = 3;

int projIDNum = 1234;
int depCode = 1234000001;
int caseNum = 45678;
NSString *result = [client report_search:employeeID case_no:caseNum status:loadFlag
day:days project_id:projIDNum dep_code:depCode count:10];
if ([result isEqualTo:@"[]"]) {
[Utilities callAlert:self title:@"No More Data" body:@"There is no other data contains"];
[refresh endRefreshing];
} else {
NSData *jsonData = [result dataUsingEncoding: NSUTF8StringEncoding];
NSError *error;
[resultArr removeAllObjects];
// resultArr is NSMutableArray and it's the tableview's data source
resultArr = [[NSJSONSerialization JSONObjectWithData:jsonData options: 0
error:&error] mutableCopy];
[refresh endRefreshing];
[_mSearchTableView reloadData];
}
}


If I get full 10 data result, the program itself won't crash but it happens when I get less than 10 datas from web service. I kept getting this exception,
NSRangeException, reason: index 8 beyond bounds [0 .. 4]
but don't know how to deal with it.

My tableview setting:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger) table:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [resultArr count];
}


This is my cellForRowAtIndexPath – Tj3n

-(UITableViewCell) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

// clear cell
for ([UIView *view in [cell.contentView subviews]]) {
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}

UILabel *titleLabel = [[UILabel alloc] init];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
} else {
titleLabel = [cell.contentView viewWithTag:100];
}

CGRect location = CGRectMake(20, 20, 71/75, 40/146);
titleLabel = [[UILabel alloc] initWithFrame:location];
titleLabel.textAlignment = NSTextAlignmentNatural;
titleLabel.text = [resultArr valueForKey:@"title"][indexPath.row]; // this is the line always crashed
[cell.contentView addSubview:titleLabel];

return cell;
}


also updating my json:

print out result: [
{
Content = "Test\nTest\n";
"Create_Datetime" = "2016-09-12 10:18:25";
"Project_ID" = 2023;
Status = 4;
"CASE_NO" = 17704;
Title = "Table title";
"Update_Datetime" = "2016-09-12 10:19:09";
},
{
Content = "Fhujhfdfhjoookbvv\nGu...";
"Create_Datetime" = "2016-09-12 10:09:28";
"Project_ID" = 2023;
Status = 4;
"CASE_NO" = 17703;
Title = "Table title";
"Update_Datetime" = "2016-09-12 10:12:59";
},
{
Content = dafsdfasfasfas;
"Create_Datetime" = "2016-09-04 01:52:09";
"Project_ID" = 2023;
Status = 5;
"CASE_NO" = 16512;
Title = "Table title";
"Update_Datetime" = "2016-09-14 16:44:46";
},
{
Content = dafsdfa;
"Create_Datetime" = "2016-09-04 01:41:59";
"Project_ID" = 2023;
Status = 4;
"CASE_NO" = 16511;
Title = "Table title";
"Update_Datetime" = "2016-09-04 01:42:23";
},
{
Content = sdfasfasf;
"Create_Datetime" = "2016-09-02 14:14:39";
"Project_ID" = 2023;
Status = 2;
"CASE_NO" = 16502;
Title = "Table title Table subtitle";
"Update_Datetime" = "2016-09-02 15:17:50";
}]


UPDATE:
I've tried to print out the log for the array count in different places before crash.

First place is
before table reload
and the number of objects in the array is 5.

Second place is in the
cellForRowsAtIndexpath
. I had 2 items to print out


  1. total count of the resultArr is 5, which is consist with the array
    count before reload.

  2. the current indexpath.row. The odd thing happened in here is that it's at 8th cell rather than 5th.



Lastly, the place I print out log is at
numberOfRowsInSection
. Before the app crashed, it does not print out the log.

Answer

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array'

Thank you, JingJingTao and Tj3n for helping me out.

I finally found the answer for this bug but I still can't figure out why and the cause for this one. From logic, it should go to numberOfRowsInSection first to setup the number of row for current table but apparently it did not. Can someone care to explain why this is?