0

我将这些数组设置为进入 a NSDictionary,然后将它们添加到NSMutableArray. 这是设置部分。

注意:数组的内容有点随机,因为我只是在尝试一些东西。

现在我期待的是“太棒了!”这个词。只会重复 3 x 3 次,因为 和 的计数NSMutableArrayheadArray只有 3。

那么为什么当我使用操作符符号时它会重复 4 x 3 次<=呢?这不应该做 3 次,因为它匹配的计数只有 3 次?

现在我很欣赏节数设置为 0,这可能是一个解释,但它需要为 0 才能位于 UITableView 的顶部。

listOfItems = [[NSMutableArray alloc] init];
headerArray = [[NSArray alloc] initWithObjects:@"Country 1", @"Country 2", @"Country 3", nil];

NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Italy", @"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

NSArray *thirdArray = [NSArray arrayWithObjects:@"Row 01", @"Row 02", @"Row 03", @"Row 04", nil];
NSDictionary *thirdDictionary = [NSDictionary dictionaryWithObject:thirdArray forKey:@"Countries"];

if (headerArray.count == listOfItems.count)
{
    for (section = 0; section <= listOfItems.count; section++)
    {
        NSLog(@"AWESOME!!");
    }
}

2013-02-17 20:10:06.323 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.324 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.325 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.326 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.327 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.328 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.330 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.331 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.332 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.332 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.334 TableView[3667:11303] AWESOME!!
2013-02-17 20:10:06.335 TableView[3667:11303] AWESOME!!
4

1 回答 1

2

不,您正在做的事情适用于 1 索引数组。NSArray但是,是 0 索引的。在你的 for 循环中发生的事情是section0to listOfItems.count,即从 0 到 3,这意味着 4 次迭代(一个分别代表 0、1、2 和 3)。请改用<运算符。

请参阅有关半开区间和从零开始的编号的Wikipedia 文章。

于 2013-02-17T07:29:29.653 回答