对不起,如果这之前已经问过,但我找不到我的问题的答案。拉到刷新结果应用程序崩溃:NSRangeException,原因:索引8超出界限[0..4]
我写了拉刷新的代码,但它崩溃如果结果数组数不同于indexPath.row
,我试图删除我的数组对象,但它仍然崩溃。
我的代码去为这个:
-(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];
}
}
如果我得到充分的10个数据结果,程序本身不会崩溃,但是当我从网络服务不到10个DATAS它发生。我一直得到这个例外,NSRangeException, reason: index 8 beyond bounds [0 .. 4]
,但不知道如何处理它。
我的实现代码如下设置:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger) table:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [resultArr count];
}
这是我的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;
}
也更新我的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: 我试着打印arr的日志在撞车前不同地方计数。
第一名是before table reload
以及阵列中的对象的数量是5。
第二位置是在cellForRowsAtIndexpath
。我不得不2项打印出
- 的resultArr的总计数是5,其是由具有重载之前阵列 计数。
- 当前indexpath.row。这里发生的奇怪事情是它在第8个单元而不是第5个。
最后,我打印出日志的地方是numberOfRowsInSection
。应用程序崩溃之前,它不打印日志。
发布您的'cellForRowAtIndexPath' – Tj3n
更新已发布 –
您不需要清除单元格,但它似乎正常,您可以设置断点并查看导致崩溃的行? – Tj3n