0

I want to remove UITableViewCells from my TableView if the userName "Brittany" appears more than once in my NSMutableArray. E.g. only the first cell displaying userName "Brittany" should appear. If my array self.messages contains userName "Brittany" more than once, I only want to show the first cell. How can I do this? See code below so far:

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *PointsTableIdentifier = @"MyMessagesCell";

    MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


        NSMutableArray* NameDictionary=[[NSMutableArray alloc] init];

        NSString* PreviousName;

        int oneTime=0;

        for(int i=0;i<[self.messages count];i++){

            NSDictionary *fromUser = [self.messages objectAtIndex: i];

            NSString *userName = [fromUser objectForKey:@"name"];

            if(oneTime==0)
            {
                NSLog(@"TEST!");
                PreviousName=userName;

                [NameDictionary addObject:[self.messages objectAtIndex: i]];

                oneTime=1;
            }

            if(oneTime!=0)
            {

                if([PreviousName isEqualToString: userName])
                {

                    NSLog(@"TEST 1!");
                }

                else{

                    [NameDictionary addObject:[self.messages objectAtIndex: i]];
                    NSLog(@"TEST 2!");
                }

                PreviousName=userName;
            }
        }


    }




    NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row];

    NSString *messageSubject = [receivedSubjectLine objectForKey:@"node_title"];


    [cell.subjectLine setText:messageSubject];



    NSDictionary *fromUser = [self.messages objectAtIndex:indexPath.row];

    NSString *userName = [fromUser objectForKey:@"name"];

    [cell.senderName setText:userName];




    NSDictionary *receivedBody = [self.messages objectAtIndex:indexPath.row];

    NSString *messageBody = [receivedBody objectForKey:@"body"];

    [cell.fullMessage setText:messageBody];




    NSDictionary *messageReceived = [self.messages objectAtIndex:indexPath.row];

    NSString *timeReceived = [messageReceived objectForKey:@"published at"];

    NSLog(@"Message Received at %@", timeReceived);

    [cell.receivedStamp setText:timeReceived];

    return cell;

}
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • Check out this question that adresses removing duplicates: https://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from-nsmutablearray-in-objective-c – Tamás Sengel Oct 02 '17 at 21:20
  • @the4kman The answer in your link doesn't seem to work for me - it's also nearly 8 years old (might be why?) – Brittany Oct 02 '17 at 21:31
  • Creating an array of dictionary objects inside the table view delegate method doesn't sound like a good plot. – El Tomato Oct 02 '17 at 21:31
  • @ElTomato I feel like the solution should be simple - isn't there a way to simply write: if the same userName appears in more than one cell, hide all latter cells? Lol – Brittany Oct 02 '17 at 21:32
  • Your TableView must correspond to model array in the way where every item from array is data for every cell in table view. Thus you just need to remove duplicates from model array and reload table view. – kirander Oct 02 '17 at 22:11
  • @kirander I'm asking how to remove duplicates...I know that's what I need to do; I'm asking because I'm not sure how lol :) – Brittany Oct 02 '17 at 22:12
  • Voted your answer down because it is not clear in it that you want just to remove duplicates from array. You are talking about table view in it. – kirander Oct 03 '17 at 12:28

1 Answers1

1

You should not try to display non-duplicate items from an array into a table view. That's a recipe for disaster.

Instead, create a removeDuplicates function that takes an input array and returns a new array.

Have the function create an empty mutable set. Loop through the source array from beginning to end. For each object from the source array, if it is not in the set, append it to both the test set and the destination array. If it is in the test set, skip it.

Note that the above will only work if the objects in the array implement the isEqual method and return a meaningful value. By default I believe generic NSObjects return isEqual == true if you are comparing the exact same object. Other classes like NSString and NSNumber implement an isEqual method that actually compares the value. If you're comparing complex objects using something like a string userName, you might need to create a custom object type (e.g. UserObject) that has a custom implementation of isEqual.

That should be enough to get you started. If you get stuck, post your code an an explanation of the problems you're having.

Duncan C
  • 128,072
  • 22
  • 173
  • 272