カスタム オブジェクトの NSMutableArray の反復処理で問題が発生しています。次のコードを実行すると、NSMutableArray オブジェクトに 2 つのオブジェクトしかないにもかかわらず、配列が 6 回繰り返されます。
//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;
}];
}
}];
この問題に関するヘルプをいただければ幸いです。