Populate TableView based on selected user

Discussion in 'iOS Programming' started by cvbfgt18, Aug 20, 2014.

  1. cvbfgt18 macrumors newbie

    Joined:
    May 1, 2014
    #1
    Hey all,

    I am developing an app with core data which allows the user to add and switch between multiple user "accounts". The main functionality of the app is as such:
    • The user creates a new "event" and certain properties of the event are stored in an entity. One of the properties is "user" which is stored in the user attribute. This records whatever user account is selected at the current time in the app
    • The user can then view the events they have created in a table view. But only the events which were created under the same account as the current account are to be shown.

    Right now, I have only been able to show/hide cell text labelling based on this principle (the table view is still populated by every event, just only the events that match the user have their labels - see photo). I want to be able to only show the events that match, without any gaps in between.

    [​IMG]


    My code for the table view:

    Code:
    - (NSManagedObjectContext *)managedObjectContext
    {
        NSManagedObjectContext *context = nil;
        id delegate = [[UIApplication sharedApplication] delegate];
        if ([delegate performSelector:@selector(managedObjectContext)]) {
            context = [delegate managedObjectContext];
        }
        return context;
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
        self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
        
        [self.tableView reloadData];
        
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return self.devices.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        
        
        // Configure the cell...
        NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
        
    
        
        NSString *comparison = [NSString stringWithFormat:@"%@", [device valueForKey:@"user"]];
        
        if ([userSelection  isEqualToString:comparison]){
        
            [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@ %@", [device valueForKey:@"goals"], [device valueForKey:@"passes"], [device valueForKey:@"shots"], [device valueForKey:@"date"]]];
            
        }
            
        return cell;
        
        
    }
    Any help appreciated.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    You need to change what data is returned by the UITableView's data source. The data source should return different row data depending on which user is active. It will also have a different row count depending on which user is active.

    For example, if there are two users, Jack and Jill, then when Jack is the active user, the only rows returned by the data source are the one's belonging to Jack. None of Jill's data appears at all when Jack is the user. If Jack has 3 items and Jill has 5, then the row-count is 3 for Jack and 5 for Jill, never 8.
     

Share This Page