【问题标题】:*** -[__NSArrayM objectAtIndex:]: index 51 beyond bounds [0 .. 50] error when I use UIsearchbar and UItableview*** -[__NSArrayM objectAtIndex:]: index 51 beyond bounds [0 .. 50] 当我使用 UIsearchbar 和 UItableview 时出错
【发布时间】:2014-03-11 04:42:00
【问题描述】:
 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
 {
if (searchText.length == 0)
    isActive = NO;
else
    isActive = YES;

NSMutableArray *tmpSearched = [[NSMutableArray alloc] init];
NSMutableArray *tmpSearchedNumber = [[NSMutableArray alloc]init];
NSUInteger count1=0;
//NSUInteger count2=0;

for(count1=0;count1<[tableStationData count] || [tableData count];count1++)
{
    NSRange range = [[tableStationData objectAtIndex:count1] rangeOfString:searchText options:NSCaseInsensitiveSearch];
    NSRange range1= [[tableData objectAtIndex:count1]rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if(range.location !=NSNotFound || range1.location != NSNotFound)
    {
        [tmpSearchedNumber addObject:[tableStationData objectAtIndex:count1]];
        [tmpSearched addObject:[tableData objectAtIndex:count1]];
    }
}

其中 tableData 数组大小为:2530 和相同的数组大小 tableStationData。当我在 UISearchbar 上搜索时,编码会出错。

【问题讨论】:

  • 您的 for 循环说取 tableStationData 计数或 tableData 计数。并且在循环中,您正在为两者取范围。这有点棘手。 tableData 和 tableStationData 是否包含相同数量的元素?如果不是,那么这是您无法获得这样的范围的问题,您应该在循环中添加一些条件。如果我的猜测是正确的,请告诉我我会发送代码
  • 是的,两个数组都有相同的元素,当我在 uisearchbar 中搜索时,它会给出错误。 @CharanGiri

标签: ios iphone objective-c uitableview uisearchbar


【解决方案1】:

我会改变这个

for(count1=0;count1<[tableStationData count] || [tableData count];count1++)

为此

for(count1=0;count1<[tableStationData count] && count1<[tableData count];count1++)

编辑:用 rmaddy 的 ovservation 更正了最后一行。

如果tableData 至少有 1 个元素,那么您使用循环条件的方式将始终为真,并且循环永远不会退出。

【讨论】:

  • 条件错误。应该是count1&lt;[tableStationData count] &amp;&amp; count1&lt;[tableData count]。使用&amp;&amp;,而不是,
  • @rmaddy 你说得对,我忽略了这一点(我习惯了在for(;;) 语句的其他两部分中允许使用逗号
  • 这些不是逗号,而是分号。
  • @rmaddy 我指的是允许使用这样的逗号:for (i=0,j=0; condition; i++,j++);
【解决方案2】:

在这一行:

for(count1=0;count1<[tableStationData count] || [tableData count];count1++)

这部分:

 || [tableData count]

如果 tableData 数组中至少有一个元素,则始终为 true,因此它将超过 tableStationData 的最大值

【讨论】:

    【解决方案3】:

    问题出在 for 循环中。假设 tableStationData 有 15 个元素,tableData 有 10 个元素。循环运行到 count1 为 9,当 count1 为 10 时,循环正在运行,但在循环内,[tableStationData objectAtIndex:count1] 工作正常,[tableData objectAtIndex:count1] 不起作用,tableData 只有 10 个元素,它没有t 有第 11 个元素。所以在这种情况下我们会得到错误,比如-[__NSArrayM objectAtIndex:]:

    for(count1=0;count1<[tableStationData count] || [tableData count];count1++)
    {
        NSRange range = [[tableStationData objectAtIndex:count1] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange range1= [[tableData objectAtIndex:count1]rangeOfString:searchText options:NSCaseInsensitiveSearch];
    }
    

    【讨论】:

    • 您阅读的代码有误。仔细查看for 循环中的条件。问题是条件的后半部分(|| 之后)没有使用count1 变量。 || 应该是 &amp;&amp; 以确保循环根据较短的数组停止。
    • 在你的情况下,这是正确的。我正在解释不同的方式。如果他像这样使用 for 循环,我说的是内部循环。他应该得到错误。我正在向他解释他会在哪里出错。所以间接地,我告诉他检查 for 循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多