Domanda

Let me allow to describe the scenario first. i have an array called "exibitors" which hold all exibitors information (all array information is parsed from an URL). Currently it has "107" Fair01 item (fairId = Fair01) & "2" Fair02 item (fairId = Fair02). Now i want to run a method which return an array. In this method i will select either Fair01 or Fair02 by checking them with a "FairIdPassing" of current ViewController and add the current "FairId" corresponding data into this method array named "exibitorsWithFairIdComplete".

Here is the method :

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

I call the method in "tableView numberOfRowsInSection:", because i want to show them in a table view. The code is :

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

But the problem is, when i call the method and want to add object to the array one by one for specific FairId, it only added the last value. And show its corresponding information.

Example :

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)

After calling this method it only print the last "B" related information in tow table rows, Like :

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

when i want showing all fairInfo one by one. Like :

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

I cannot point out the mistake happen in addingObject to an MutableArray. Please question me if i am not make this post clearly to you. If any one familiar with this problem, please let me know.

È stato utile?

Soluzione 2

This should make your business :

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

You were allocating an ExibitorInfo object, then throwing it out, accessing the object in the array and then mutating currentExibitors2 and inserting it in your array. But as the insertion didn't make a copy, you were in fact changing and insering the same object again and again.

Altri suggerimenti

Try something like this:

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

Hope it helps you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top