indexPath.row 抛出异常

发布于 2024-12-13 09:03:03 字数 227 浏览 3 评论 0原文

我正在切换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-12-20 09:03:03

问题在于索引 indexPath.row 大于 secondArrayfirstArray 中存在的索引。要解决根本原因,您需要找出数组中缺少该元素的原因。为了确保它不会崩溃并仅在标签的文本属性中加载空字符串,请检查数组中的最大索引是否大于或等于 indexPath.row

if (_secondTab.selected) { 
    cell.customLabel.text = (indexPath.row <= ([secondArray count]-1)) ? [[secondArray objectAtIndex:indexPath.row] elementName] : @"";
} else {
    cell.customLabel.text = (indexPath.row <= ([firstArray count]-1)) ? [[firstArray objectAtIndex:indexPath.row] elementName] : @"";
}

The problem is that the index indexPath.row is larger than what exists in either secondArray or firstArray. 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 to indexPath.row:

if (_secondTab.selected) { 
    cell.customLabel.text = (indexPath.row <= ([secondArray count]-1)) ? [[secondArray objectAtIndex:indexPath.row] elementName] : @"";
} else {
    cell.customLabel.text = (indexPath.row <= ([firstArray count]-1)) ? [[firstArray objectAtIndex:indexPath.row] elementName] : @"";
}
回忆那么伤 2024-12-20 09:03:03

当索引值超出范围时,可能会发生这种情况。确保 secondaryArray 和firstArray 具有相同数量的值。或者用你的逻辑来验证它是否超出范围?

例如,如果数据源是 secondaryArray,则您的 indexPath.row 是 4

firstArray 有 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 4

firstArray has 3 values, secondArray has 6 values. In that situation [firstArray objectAtIndex:4] will throw an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文