Populating UITableView cells.
===

Ok so I have wrote some code in this function:


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"tableviwcell is getting called");
        properties = [NSUserDefaults  standardUserDefaults];
        
        NSDictionary *events_dict = [properties objectForKey:@"events_dict"];
        NSDictionary *events = [events_dict objectForKey:@"event"];
    
        am_participants = [events objectForKey:@"amParticipants"];
        pm_participants = [events objectForKey:@"pmParticipants"];
        
        UITableViewCell *cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:@"currentCell"];
        if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"currentCell"];
        }
        int sizeofam = [am_participants count];
        int sizeofpm = [pm_participants count];
        NSMutableArray *participants  =[[NSMutableArray alloc] init];
        NSLog(@"size of am: %i", sizeofam);
    if(sizeofam > 0 && sizeofpm > 0){
        for(int i = 0; i < sizeofam; i++){
            NSDictionary *ampart_dict = [am_participants objectAtIndex:i];
            NSString *ampart_email = [ampart_dict objectForKey:@"email"];
            NSString *ampart_email_extra = [@"am" stringByAppendingString:ampart_email];
            [participants addObject:ampart_dict];
        }
        
        for(int i = 0; i < sizeofpm; i++){
            NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i];
            NSString *pmpart_email = [pmpart_dict objectForKey:@"email"];
            NSString *pmpart_email_extra = [@"pm" stringByAppendingString:pmpart_email];
            [participants addObject:pmpart_dict];
            NSLog(@"%@", participants);
        } 
        
        int sizeofparticipants = [participants count];
        
        for(int i = 0; i < sizeofparticipants; i++){
            NSString *participanti = [[participants objectAtIndex:i] objectForKey:@"email"];
            for(int j = 0; j < sizeofparticipants; j++){
                NSString *participantj = [[participants objectAtIndex:j] objectForKey:@"email"];
                
                if([participanti isEqualToString:participantj]){
                    NSLog(@"Participant j:  %@", participantj);
                    [participants removeObjectAtIndex:j];
                    sizeofparticipants = [participants count];
                }
            }
        }

Basically I need to merge together the two arrays am_participants and pm_participants in to one array of participants. Each object in the array is a dictionary full of various values such as name, email etc. I then have to remove any duplicate values from the list (for example if someone is in the am list and pm list. I am doing that by removing values if their email address is the same in either list. 

After this I need to display the new array of participants called 'participants' in one list by populating the cells with the list of participants. 

I have set the number of cells to 30. 

However The cells get populated randomly when scrolling down the list. 

Can someone explain what I am doing wrong?

Thanks!

