NSMutableArray仅添加最后一个对象

时间:2013-07-14 09:50:33

标签: iphone ios objective-c nsmutablearray xcode4.5

让我先说明一下情景。我有一个名为“exibitors”的数组 它包含所有exibitors信息(所有数组信息都从URL解析)。 目前它有“107”Fair01项目(fairId = Fair01)& “2”Fair02项目 (fairId = Fair02)。现在我想运行一个返回数组的方法。 在这种方法中,我将通过检查它们来选择Fair01或Fair02 当前ViewController的“FairIdPassing”并添加当前的“FairId” 相应的数据到这个方法数组中,名为“exibitorsWithFairIdComplete”。

以下是方法:

-(NSMutableArray *) getFairInfoArray:(NSString *)FairIdForThisMethod From:(NSMutableArray*)exibitorsForThisMethod
{
ExibitorInfo *currentExibitors2=[[ExibitorInfo alloc] init];
exibitorsWithFairId = [[NSMutableArray alloc] init];

// "FairIdHolderCount" = how many fairId is there, in the list
int FairIdHolderCount = [exibitorsForThisMethod count];

for (int i=0; i<FairIdHolderCount; i++)
{
    ExibitorInfo *currentExibitors1=[[ExibitorInfo alloc] init];
    currentExibitors1 = [exibitorsForThisMethod objectAtIndex:i];

    if ([currentExibitors1.FairId isEqualToString:FairIdForThisMethod])
    {
        [currentExibitors2 setExibitorName:currentExibitors1.exibitorName];
        [currentExibitors2 setStallLocation:currentExibitors1.stallLocation];
        [currentExibitors2 setExibitorCellBackGround:currentExibitors1.exibitorCellBackGround];
        [currentExibitors2 setCompanyLogoURL:currentExibitors1.companyLogoURL];

     [exibitorsWithFairId addObject:currentExibitors2];   
    }
}
   return exibitorsWithFairId;
}

我在“tableView numberOfRowsInSection:”中调用该方法,因为我想 在表格视图中显示它们。代码是:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // call the method
    exibitorsWithFairIdComplete = [self getFairInfoArray:FairIdPassing From:exibitors];
    return [self.exibitorsWithFairIdComplete count];
}

但问题是,当我调用该方法并想要添加对象时 对于特定的FairId逐个数组,它只添加了最后一个值。 并显示其相应的信息。

示例:

For Fair02 ----> "exibitors" array contain 2 "Fair02" elements besides 107 "Fair01": 

(in array)
fair id : Fair02, fairName : A, Location: A
fair id : Fair02, fairName : B, Location: B


(fair id : Fair01, fairName : X, Location: X
fair id : Fair01, fairName : Y, Location: Y
fair id : Fair01, fairName : Z, Location: Z
.......
.......
.......
.......
.......
total 107)

调用此方法后,它只打印最后一个“B”相关 拖曳表行中的信息, 喜欢:

"fair id : Fair02, fairName : B, Location: B"
"fair id : Fair02, fairName : B, Location: B"

当我想逐个显示所有fairInfo时。 喜欢:

fair id : Fair02, fairName : A, Location: A
fair id : Fair02, fairName : B, Location: B

我不能指出在将对象添加到MutableArray时会发生错误。 如果我没有清楚地向你发这个帖子,请问我。如果有的话 熟悉这个问题,请告诉我。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

- (NSMutableArray*) getFairInfoArray: (NSString*) FairIdForThisMethod From: (NSMutableArray*) exibitorsForThisMethod
{
    NSMutableArray* exibitorsWithFairId = [[NSMutableArray alloc] init];

    for (ExibitorInfo* currentExibitors1 in exibitorsForThisMethod)
    {
        if ([currentExibitors1.FairId isEqualToString: FairIdForThisMethod])
        {
            ExibitorInfo* currentExibitors2 = [[ExibitorInfo alloc] init];
            [currentExibitors2 setExibitorName:currentExibitors1.exibitorName];
            [currentExibitors2 setStallLocation:currentExibitors1.stallLocation];
            [currentExibitors2 setExibitorCellBackGround:currentExibitors1.exibitorCellBackGround];
            [currentExibitors2 setCompanyLogoURL:currentExibitors1.companyLogoURL];

            [exibitorsWithFairId addObject:currentExibitors2];
        }
    }

    return exibitorsWithFairId;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    self.exibitorsWithFairIdComplete = [self getFairInfoArray: FairIdPassing
                                                         From: exibitors];

    return [self.exibitorsWithFairIdComplete count];
}

希望它对你有所帮助。

答案 1 :(得分:0)

这应该是您的业务:

-(NSMutableArray *) getFairInfoArray:(NSString *)FairIdForThisMethod From:(NSMutableArray*)exibitorsForThisMethod {
    NSMutableArray *exibitorsWithFairId = [[NSMutableArray alloc] init];
    // "FairIdHolderCount" = how many fairId is there, in the list
    int FairIdHolderCount = [exibitorsForThisMethod count];

    for (ExibitorInfo *currentExibitors1 in exibitorsForThisMethod) {
        if ([currentExibitors1.FairId isEqualToString:FairIdForThisMethod]) {
           ExibitorInfo *currentExibitors2=[[[ExibitorInfo alloc] init] autorelease];

           [currentExibitors2 setExibitorName:currentExibitors1.exibitorName];
           [currentExibitors2 setStallLocation:currentExibitors1.stallLocation];
           [currentExibitors2 setExibitorCellBackGround:currentExibitors1.exibitorCellBackGround];
           [currentExibitors2 setCompanyLogoURL:currentExibitors1.companyLogoURL];

           [exibitorsWithFairId addObject:currentExibitors2];   
        }
     }

     return exibitorsWithFairId;
 }

您正在分配一个ExibitorInfo对象,然后将其丢弃,访问数组中的对象,然后改变currentExibitors2并将其插入您的数组中。但由于插入没有制作副本,实际上你一次又一次地改变和插入同一个对象。