objective-c - 迭代 NSMutableArray 无法正常工作

标签 objective-c ios nsmutablearray

我在迭代自定义对象的 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;
        }];
    }
}];

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

最佳答案

我是 JMStaticContentTableViewController 的创建者。

addCell:方法没有被多次调用。 JMStaticContentTableViewCellBlock阻止您进入 addCell:方法作为 tableView:cellForRowAtIndexPath: 的一部分被调用 Controller 的方法。

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

检查完上面的代码后,我建议您迭代两次,一次配置您的 twitterSwitchesArray正确地,另一个包含您想要在 tableView:cellForRowAtIndexPath: 中调用的代码方法。

您可以查看执行 for 循环并在 JMStaticContentTableViewCellBlock 内运行循环安全代码的示例。 right here .

JMStaticContentTableViewController设计的目的是让您仍然可以像传统的基于委托(delegate)的方式一样“思考”它 UITableViewController模型,但使用 block 代替。

这就是为什么 JMStaticContentTableViewCellBlock传入 UITableViewCell例如,这样您就可以像从 dequeueReusableCellWithIdentifier: 获得的一样对待它。方法UITableView 。它还传入 JMStaticContentTableViewCell模型对象实例,以便您可以配置一些在标准 UITableViewCell 上没有意义或不可能的事情。对象,例如 tableViewCellSubclasscellHeight .

希望这有帮助!

关于objective-c - 迭代 NSMutableArray 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12738095/

相关文章:

objective-c - iOS下的Unicode问题

ios - NSMutableArray 索引变得非常高并且 removeObjectAtIndex 超出范围

iphone - 将纬度和经度转换为 x 和 y 坐标

ios - 如何计算真实世界的对象大小?

objective-c - Objective C xpath 获取多个标签值

ios - Objective-C: "id = (property) ? property : defaultObject;"的快捷方式 - 它存在吗?

ios - 在 block 内访问 BOOL

ios - 持续集成,如何为 QA 和 App Store 发布生成单个构建(二进制)?

ios - 关于插入和删除单元格的 UITableView 动画问题

ios - 为什么我可以将消息发送到已释放的 NSArray 实例?