NSMutableArray仅添加最后一个对象
我首先允许描述该方案。我有一个名为“exibitors” 的数组,其中包含所有exibitors信息(所有数组信息都从URL中解析出来)。 目前它有“107”Fair01项目(fairId = Fair01)&“2”Fair02项目 (fairId = Fair02)。现在我想运行一个返回数组的方法。 在这种方法中,我将通过使用当前ViewController的“FairIdPassing”检查它们来选择Fair01或Fair02,并将当前的“FairId” 相应数据添加到名为“exibitorsWithFairIdComplete”的此方法数组中。NSMutableArray仅添加最后一个对象
这里是方法:
-(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
我不能指出在addingObject错误发生的MutableArray。 请问我,如果我不明确这个帖子给你。如果有任何一位 熟悉这个问题,请让我知道。
这应该使您的业务:
-(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
和数组中插入。但是由于插入没有复制,你实际上正在改变和一次又一次地插入同一个对象。
真棒.....你让我的一天....:)我知道了,它完美的工作。非常感谢,@ rems4e ..... :) :) :),你是男人。 – Tulon
尝试这样:
- (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];
}
希望它可以帮助你。
感谢您的评论,但我已经得到了解决方案。许多非常感谢您的合作:) @stosha – Tulon
为什么你在'
'标签中包装你的问题的散文部分? –视觉舒适。 – Tulon
最终的结果是五块灰色,看起来都像代码。这是怎么样的视觉舒适? –