0

我在迭代自定义对象的 NSMutableArray 时遇到问题。每当我运行以下代码时,它都会对数组进行 6 次迭代,尽管 NSMutableArray 对象中只有 2 个对象。

//Configure the Settings Screen
[self addSection:^(JMStaticContentTableViewSection *section, NSUInteger sectionIndex) {

    section.title = @"Twitter Accounts";

    //alloc and init the twitterSwitchesArray to store the switches used in the view
    self.twitterSwitchesArray = [[NSMutableArray alloc] initWithCapacity:[self.twitterAccounts count]];

    NSLog(@"LOADING: twitterAccounts Array count: %i", [self.twitterAccounts count]);

    for(MessageBlastSavedTwitterAccount *twitterAccount in _twitterAccounts)
    {
        NSLog(@"Configuring: %@", twitterAccount.twitterAccountDescription);

        //Configure the Twitter Setting Switch
        [section addCell:^(JMStaticContentTableViewCell *staticContentCell, UITableViewCell *cell, NSIndexPath *indexPath) {

            UISwitch *twitterSwitch = [[UISwitch alloc] init];
            twitterSwitch.accessibilityLabel = twitterAccount.twitterAccountDescription;
            twitterSwitch.on = [self switchShouldBeFlippedForTwitterAccount:twitterAccount.twitterAccountDescription];

            NSLog(@"LOADING: Twitter account %@, should be enabled: %i", twitterAccount.userEnteredDescription, [self switchShouldBeFlippedForTwitterAccount:twitterAccount.twitterAccountDescription]);

            [self.twitterSwitchesArray addObject:twitterSwitch];

            [twitterSwitch addTarget:self action:@selector(flippedTwitterSwitch:) forControlEvents:UIControlEventValueChanged];

            staticContentCell.reuseIdentifier = @"UIControlCell";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = [twitterAccount userEnteredDescription];
            cell.imageView.image = [UIImage imageNamed:@"210-twitterbird.png"];
            cell.accessoryView = twitterSwitch;
        }];
    }
}];

我得到以下输出:

2012-10-04 20:47:27.703 MessageDraft[1206:c07] LOADING: twitterAccounts Array count: 2
2012-10-04 20:47:27.703 MessageDraft[1206:c07] Configuring: coryb
2012-10-04 20:47:27.704 MessageDraft[1206:c07] LOADING: Twitter account @coryb, should be enabled: 1
2012-10-04 20:47:27.705 MessageDraft[1206:c07] Configuring: cocoaapp
2012-10-04 20:47:27.706 MessageDraft[1206:c07] LOADING: Twitter account @cocoaapp, should be enabled: 1
2012-10-04 20:47:27.708 MessageDraft[1206:c07] LOADING: Twitter account @coryb, should be enabled: 1
2012-10-04 20:47:27.709 MessageDraft[1206:c07] LOADING: Twitter account @cocoaapp, should be enabled: 1
2012-10-04 20:47:27.713 MessageDraft[1206:c07] LOADING: Twitter account @coryb, should be enabled: 1
2012-10-04 20:47:27.714 MessageDraft[1206:c07] LOADING: Twitter account @cocoaapp, should be enabled: 1

我什至尝试过手动方式(如下)来迭代数组,但我仍然遇到完全相同的问题:

// The self.twitterAccounts count returns a 2, so this should only repeat twice.
    for(int i = 0; i < [self.twitterAccounts count]; i++)
    {
        NSLog(@"%i", i);

        //Configure the Twitter Setting Switch
        [section addCell:^(JMStaticContentTableViewCell *staticContentCell, UITableViewCell *cell, NSIndexPath *indexPath) {

            UISwitch *twitterSwitch = [[UISwitch alloc] init];
            twitterSwitch.tag = i;
            twitterSwitch.on = [self switchShouldBeFlippedForTwitterAccount:_twitterAccount.description];

            self.twitterAccount = [_twitterAccounts objectAtIndex:i];

            NSLog(@"LOADING: Twitter account %@, should be enabled: %i", _twitterAccount.userEnteredDescription, [self switchShouldBeFlippedForTwitterAccount:_twitterAccount.description]);

            [self.twitterSwitchesArray addObject:twitterSwitch];

            [twitterSwitch addTarget:self action:@selector(flippedTwitterSwitch:) forControlEvents:UIControlEventValueChanged];

            staticContentCell.reuseIdentifier = @"UIControlCell";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.tag = i;
            cell.textLabel.text = [_twitterAccount userEnteredDescription];
            cell.imageView.image = [UIImage imageNamed:@"210-twitterbird.png"];
            cell.accessoryView = twitterSwitch;
        }];
    }
}];

任何有关此问题的帮助将不胜感激!

4

3 回答 3

1

我是JMStaticContentTableViewController的创建者。

addCell:方法不会被多次调用。JMStaticContentTableViewCellBlock您传递给方法的块作为控制器方法的一部分 addCell:被调用。tableView:cellForRowAtIndexPath:

它这样做是为了让您可以正确配置UITableViewCells并有效地重用它们。

在查看了上面的代码之后,我建议您迭代两次,一次是为了twitterSwitchesArray正确配置您的代码,另一次是使用您希望在tableView:cellForRowAtIndexPath:方法中调用的代码。

您可以在JMStaticContentTableViewCellBlock 此处查看执行 for 循环并在右侧运行循环安全代码的示例。

JMStaticContentTableViewController被设计成您仍然可以像传统的基于委托的UITableViewController模型一样“思考”它,但使用块来代替。

这就是为什么JMStaticContentTableViewCellBlock传入一个UITableViewCell实例,这样你就可以把它当作你dequeueReusableCellWithIdentifier:UITableView. 它还传入模型对象实例,以便您可以配置一些在标准对象JMStaticContentTableViewCell上没有意义或不可能的东西,例如or 。UITableViewCelltableViewCellSubclasscellHeight

希望这可以帮助!

于 2012-10-05T01:51:57.783 回答
0

我认为问题不在于for循环,甚至不在于这种方法,而在于addCell:. 您设置的块addCell:被多次调用。

于 2012-10-05T01:04:06.487 回答
0

这是内部问题,最好使用 ((ClassName*)[mutableArray objectAtIndex:i])

于 2012-10-05T02:30:34.943 回答