matthisb
matthisb

Reputation: 1118

Reload Content of UITableView

I have a problem with refreshing the content of a UITableView. I'm trying to display a list of members in a UITableView. Above this list I have some kind of dropdown-menu for a group selection. So, if I change the group, selected in the dropdown-menu, I want to change the content of the UITableView underneath it. It's actually not working correctly. The first time I load the ViewController that contains the UITableView, it shows the correct list of members in the default group. Now, when I change the group, it should display my new list of members, but it doesn't. It seems that the UITableView would not even reload its table content. Here is my code for the TableViewController:

MemberTableViewController.m

- (void)viewDidLoad {
  [super viewDidLoad];

  self.members = [NSMutableArray new];
  self.selectedRows = [NSMutableArray new];
  self.groupManager = [GroupManager new];

  int *number;
  if (nil != [self gruppeId]) {
      number = [[self gruppeId] intValue]; // Here will be the id of the new selected group
  } else {
    number = 4010292; // This is just for testing. It's the id of my default group
  }

  for (Group *group in [self.groupManager findGroupWithID:[NSNumber numberWithInt:number]]) {

    for (Member *member in group.member) {
        [self.members addObject:member];
    }
  }

  NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
  [self.gruppenMitglieder sortUsingDescriptors:[NSArray arrayWithObject:sort]];
  [self.tableView setAllowsMultipleSelection:YES];
  [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return  [[self members] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GreenTexturedCell *cell = nil;
    Member *member = [self members][indexPath.row];
    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"GreenTexturedCell" owner:nil    options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]]) {
            cell = (GreenTexturedCell*)view;
        }
    }


    cell.textLabel.text = member.name;
    cell.textLabel.font = [UIFont fontWithName:@"JandaCloserToFree" size:14.0];
    cell.textLabel.textColor = [UIColor whiteColor];

    return cell;
}

Now I have another ViewController the MemberListViewController, it contains the MemberTableViewController (I know, the naming is not the best...). The MemberListViewController implements a protocol of the GroupSelectionTableViewController. Its delegate function will be called when the selected group changes. Here is the code for the MemberListViewController: MemberListViewController.h

@interface MemberListViewController : UIViewController <GroupSelectionDelegate>
@property (weak, nonatomic) IBOutlet UIButton *groupSelection;
@property (nonatomic, strong) GroupSelectionTableViewController *groupTableView;
- (IBAction)groupSelectionPressed:(id)sender;
@end

MemberListViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setGroupTableView:[GroupSelectionTableViewController new]];
    [[self groupTableView] setDelegate:self];
}

- (IBAction)gruppeAuswahlPressed:(id)sender {

    if ([self groupListShown]) {
        [[[self groupTableView] view] removeFromSuperview];
        [self setGroupListShown:NO];
    } else {
        [self.view addSubview:[[self groupTableView] view]];
        [self setGroupListShown:YES];
    }
}

- (void) groupSelectionController:(GroupSelectionTableViewController *)groupSelection DidSelectGroupWithId:(NSString *) groupId {
    [[self groupSelection] setTitle:name forState:UIControlStateNormal];
    [[[self groupTableView] view] removeFromSuperview];
    [self setGroupListShown:NO];
    MemberTableViewController *memberTableView = [self.storyboard instantiateViewControllerWithIdentifier:@"memberTable"];
    [memberTableView setGroupId:[NSNumber numberWithInt:4017411]]; // Id for testing
    [memberTableView refreshControl];

}

Now, if I change the group selection, the -groupSelectionController:DidSelectGroupWithId: Method is called and with [memberTableView refreshControl] I'm able to get into the viewDidLoad Method of the MemberTableViewController again and set the groupId to my testing id. Now i want the TableView to reload its content to get the members of the selected group. With the statement [self.tableView reloadData] I'm getting into the tableView:NumberOfRowsInSection: Method but not into the tableView:CellForRowAtIndexPath: Method and the TableView Content is not redrawn.

What am I doing wrong ? This problem is driving me nuts, so I hope there's someone to help me out. Thanks

Upvotes: 2

Views: 547

Answers (1)

Daniel Kl&#246;ck
Daniel Kl&#246;ck

Reputation: 21147

With instantiateViewControllerWithIdentifier you are creating a new instance of MemberTableViewController, which isn't the one you are showing on screen. Hence, you won't see the changes which should be shown by calling reloadData.

To be able to reload the table data, you can add a link to the table at your MemberListViewController. Change the code of the .h to:

#import "MemberTableViewController.h"
@interface MemberListViewController : UIViewController <GroupSelectionDelegate>
{
    IBOutlet MemberTableViewController *memberTableView;
}
@property (weak, nonatomic) IBOutlet UIButton *groupSelection;
@property (nonatomic, strong) GroupSelectionTableViewController *groupTableView;
- (IBAction)groupSelectionPressed:(id)sender;
@end

and then, in the .m, you can call [memberTableView reloadData];

Do not forget to set memberTableView as "new referencing outlet" within the storyboard file.

Upvotes: 1

Related Questions