让我先描述一下场景。我有一个名为“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 时发生的错误。如果我没有向你清楚地发布这篇文章,请向我提问。如果有谁熟悉这个问题,请告诉我。