indexPath.row 抛出异常
我正在切换 tableview 的数据源,但 indexPath.row 抛出异常 [index x 超出范围]
cell.customLabel.text = [[(_secondTab.selected) ? secondArray : firstArray objectAtIndex:indexPath.row]elementName];
知道如何解决这个问题吗?
I'm toggling the datasource of an tableview but the indexPath.row throws me an exception [index x beyond bounds]
cell.customLabel.text = [[(_secondTab.selected) ? secondArray : firstArray objectAtIndex:indexPath.row]elementName];
Any idea how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于索引
indexPath.row
大于secondArray
或firstArray
中存在的索引。要解决根本原因,您需要找出数组中缺少该元素的原因。为了确保它不会崩溃并仅在标签的文本属性中加载空字符串,请检查数组中的最大索引是否大于或等于indexPath.row
:The problem is that the index
indexPath.row
is larger than what exists in eithersecondArray
orfirstArray
. To fix the root cause, you need to figure out why the element is missing from your arrays. To ensure it doesn't crash and simply loads an empty string in the label's text property, check that the largest index in the array is greater than or equal toindexPath.row
:当索引值超出范围时,可能会发生这种情况。确保 secondaryArray 和firstArray 具有相同数量的值。或者用你的逻辑来验证它是否超出范围?
例如,如果数据源是 secondaryArray,则您的
indexPath.row
是 4firstArray 有 3 个值,secondArray 有 6 个值。在这种情况下,
[firstArray objectAtIndex:4]
将引发异常。This may occur when the index value is out of range. Make sure that secondArray and firstArray has same number of values. Or else use your logic to verify that it is out of range or not?
For example, if the datasource is secondArray, your
indexPath.row
is 4firstArray has 3 values, secondArray has 6 values. In that situation
[firstArray objectAtIndex:4]
will throw an exception.