Question

I've read all similar questions and tried all suggestions, still nothing. Maybe someone can spot my flaw.

My view controller is initiated from another view controller, by one of two buttons. Button taps send NSNotification (with attached arrays), and this view controller anticipates this notification and then calls this method:

- (void)addContentToArray:(NSNotification *)aNotification {

    array = [NSArray arrayWithArray:[aNotification object]];

    ([array count] == 6) ? (category = YES) : (category = NO);

    [myTableView reloadData];
    NSLog(@"%d", [array count]);
    NSLog(@"%@", myTableView);
}

The method gets called every time, I can see that from changing array count. Here notification object is the array passed from previous view controller, and I assign these objects to my local array property - this is my UITableView source. So what I do is I try to reuse the UITableView to display elements of whatever array is being passed. And it works nicely for the first array passed (whichever first).

When I tap the second button, the new array is passed successfully (as mentioned before, I know that from log of [array count] which is different: 3 vs 6 objects in different arrays). However, what is not happening is that UITableView does not refresh (although the values passed when I select a row in the table are from the correct arrays, even though wrong values are displayed).

Here are UITableView data source methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [array count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Identifier"];
    }

    cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"name"];

    if (category) {
        cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"description"];
    }

    return cell;
}

So, what am I doing wrong?

A few other considerations that might help:

  • NSLog(@"%@", myTableView); returns (null), which is a bit worrying. myTableView here is UITableView from my nib file, which is correctly connected to the view controller, declared as property and synthesized
  • The view controller in question is a rightViewController of the PKRevealController, so when it is called repeatedly, viewWillAppear method is called, but not viewDidLoad (although, as I already mentioned, addContentToArray: method is being called every time as well)
  • Also, for those somewhat familiar with PKRevealController - when I try and log focusedController from my view controller, it says that frontViewController - the one that moves to reveal my view controller - is the one that is focused. Can that be the reason why myTableView is (null)?

I'd be grateful for any insight and help!

Was it helpful?

Solution 2

I managed to solve the issue by editing initWithNibName method (old line commented out)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    //self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    self = [super initWithNibName:@"PurposeCategoryViewController" bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

Apparently, it did have something to do with the fact that my view controller (called PurposeCategoryViewController) was not the top/focused view controller in PKRevealController hierarchy. So, I just needed to specifically indicate my nib file.

OTHER TIPS

Need more code, that part where you created and call Myviewcontroller's addContentToArray method. I think you used release code there for Myviewcontroller's object, try once with hide that part.

What value are you returning in this delegate method: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView make sure it is 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top