coryb
coryb

Reputation: 238

Iterating over NSMutableArray Not Working Properly

I'm having an issue iterating over an NSMutableArray of custom objects. Whenever I run the following code, it will iterate over the array 6 times, despite there only being 2 objects in the NSMutableArray object.

//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;
        }];
    }
}];

I get the following output:

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

I have even tried a manual way (below) to iterate over the array, and I still have the exact same issue:

// 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;
        }];
    }
}];

Any help with this issue would be greatly appreciated!

Upvotes: 0

Views: 168

Answers (3)

casillas
casillas

Reputation: 16793

it is internal issue, it would be better to use ((ClassName*)[mutableArray objectAtIndex:i])

Upvotes: 0

Jake Marsh
Jake Marsh

Reputation: 865

I'm the creator of JMStaticContentTableViewController.

The addCell: method is not getting called multiple times. The JMStaticContentTableViewCellBlock block you pass into the addCell: method gets called as part of the tableView:cellForRowAtIndexPath: method of the controller.

It does this so that you can properly configure UITableViewCells and they get reused efficiently.

After reviewing your code above, I'd suggest that you iterate through twice, once to configure your twitterSwitchesArray properly, and another with the code you'd like called in the tableView:cellForRowAtIndexPath: method.

You can look at an example of doing a for loop, and running loop-safe code inside of the JMStaticContentTableViewCellBlock right here.

JMStaticContentTableViewController is designed so that you can still "think" of it in the same way as the traditional delegate-based UITableViewController model, but using blocks instead.

That's why JMStaticContentTableViewCellBlock passes in a UITableViewCell instance, this is so you can treat it like what you would be handed from the dequeueReusableCellWithIdentifier: method of UITableView. It also passes in JMStaticContentTableViewCell model object instance so that you can configure a few things that wouldn't make sense or wouldn't be possible on the standard UITableViewCell object, like tableViewCellSubclass or cellHeight.

Hope this helps!

Upvotes: 1

John Estropia
John Estropia

Reputation: 17500

I think the problem is not the for loop, not even this method, but inside addCell:. The block you set in addCell: is getting called multiple times.

Upvotes: 0

Related Questions