-1

让我先描述一下场景。我有一个名为“exibitors”的数组,它包含所有 exibitors 信息(所有数组信息都是从 URL 解析的)。目前它有“107”Fair01 项目(fairId = Fair01)和“2”Fair02 项目(fairId = Fair02)。现在我想运行一个返回数组的方法。在此方法中,我将通过使用当前 ViewController 的“FairIdPassing”检查它们来选择 Fair01 或 Fair02,并将当前的“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 时发生的错误。如果我没有向你清楚地发布这篇文章,请向我提问。如果有谁熟悉这个问题,请告诉我。

4

2 回答 2

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

希望它可以帮助你。

于 2013-07-14T10:10:45.677 回答
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并将其插入您的数组中。但是由于插入没有复制,实际上您是在一次又一次地更改和插入同一个对象。

于 2013-07-14T10:15:56.963 回答