加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在非空数组上获取’索引0超出空数组边界’错误

发布时间:2020-12-16 06:57:34 所属栏目:百科 来源:网络整理
导读:我有这个代码在我的应用程序的根视图控制器中向表视图添加一行: NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];NSArray *myArray = [NSArray arrayWithObject:indexPath];NSLog(@"count:%d",[myArray count]);[self.tableView ins
我有这个代码在我的应用程序的根视图控制器中向表视图添加一行:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

NSArray *myArray = [NSArray arrayWithObject:indexPath];

NSLog(@"count:%d",[myArray count]);

[self.tableView insertRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

当它与模拟器一起运行时,我得到这个输出:

count:1
* Terminating app due to uncaught exception ‘NSRangeException’,reason: ‘* -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array’

这发生在[self.tableView insertRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];但是,NSLog语句显示名为myArray的NSArray不为空.我错过了什么吗?以前有没有遇到过这个?

解决方法

在日志语句之前的行中,您将创建一个仅在此方法范围内有效的新数组.您用于表视图的数据源方法的数组是不同的,因此该数组中的对象数量根本不重要,即使它(我怀疑)具有相同的名称.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读