Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在NSMutableArray上迭代无法正常工作_Objective C_Ios_Nsmutablearray - Fatal编程技术网

Objective c 在NSMutableArray上迭代无法正常工作

Objective c 在NSMutableArray上迭代无法正常工作,objective-c,ios,nsmutablearray,Objective C,Ios,Nsmutablearray,我在迭代自定义对象的NSMutableArray时遇到问题。无论何时运行以下代码,它都会在数组上迭代6次,尽管NSMutableArray对象中只有2个对象 //Configure the Settings Screen [self addSection:^(JMStaticContentTableViewSection *section, NSUInteger sectionIndex) { section.title = @"Twitter Accounts"; //al

我在迭代自定义对象的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;
        }];
    }
}];
//self.twitterAccounts计数返回2,因此这应该只重复两次。
对于(int i=0;i<[self.twitter帐户计数];i++)
{
NSLog(@“%i”,i);
//配置Twitter设置开关
[节addCell:^(JMStaticContentTableViewCell*staticContentCell,UITableViewCell*单元格,NSIndexPath*indexPath){
UISwitch*twitterSwitch=[[UISwitch alloc]init];
twitterSwitch.tag=i;
twitterSwitch.on=[自切换应适用于WitterAccount:\u twitterAccount.description];
self.twitterAccount=[[u twitterAccounts对象索引:i];
NSLog(@“加载:Twitter帐户%@,应启用:%i”,_twitterAccount.userEnteredDescription,[self-SwitchshouldbelippedForWitterAccount:_twitterAccount.description]);
[self.twitterSwitchesArray addObject:twitterSwitch];
[twitterSwitch addTarget:self action:@selector(FlippedWitterSwitch:)for ControlEvents:UIControlEventValueChanged];
staticContentCell.reuseIdentifier=@“uicontrol单元格”;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.tag=i;
cell.textLabel.text=[\u twitterAccount userEnteredDescription];
cell.imageView.image=[UIImage ImageName:@“210 twitterbird.png”];
cell.accessoryView=twitterSwitch;
}];
}
}];

在此问题上的任何帮助都将不胜感激

我认为问题不在于
for
循环,甚至不在于此方法,而在于
addCell:
内部。您在
addCell:
中设置的块将被多次调用。

我是的创建者

未多次调用
addCell:
方法。传递到
addCell:
方法中的
JMStaticContentTableViewCellBlock
块将作为控制器的
tableView:cellForRowAtIndexPath:
方法的一部分被调用

它这样做是为了让您能够正确配置
uitableviewcell
,并高效地重用它们

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

您可以查看执行for循环的示例,并在
JMStaticContentTableViewCellBlock
中运行循环安全代码

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

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


希望这有帮助

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

我不知道JMStaticContentTableViewSection是什么,但问题可能是调用addCell方法的时候。你能提供此方法的文档吗?我怀疑它在每个循环中执行不止一次。JMStaticContentTableView的文档如下:for循环的行为符合预期(迭代两次),问题在于addCell方法被重复调用。是的,看起来这就是问题所在。谢谢你的帮助。