Call viewWillAppear in a UITableviewController

I am trying to call a function [MyTableviewController.tableview reloaddata]for my UITableView. I figured the best place to do this is to -(void) viewWillAppearsubclass the UITableViewController method .

When creating the class, the code was somehow not created, and I tried to implement this method with little success.

After doing some research on SO and various other websites, I found that the problem might be that the UIViewController subclass is part of the navigation controller, which in turn is part of the tab bar controller. The general sentence and code was intended to subclass one (which?) Controllers and implement the message -viewWillAppear.

My questions: 1. Is there a way to call this much needed method WITHOUT subclassing another controller? 2. If so, how should I do it? 3. If not, could you explain to me what I should do, and more importantly, why should I do this?

Gets the full UITableViewController code:

//
//  OverViewController.m
//  NoificationTest
//
//  Created by Mirko Winckel on 15.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "OverViewController.h"
#import "SecondOverViewController.h"
#import "Globals.h"

@interface OverViewController ()

@end

@implementation OverViewController

@synthesize entrys;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView reloadData];

    if (entrys == nil) 
    {

    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* fileName = [[Globals sharedGlobals].selectedProject stringByAppendingString:@".csv"];
    NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
    NSString* content = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding];
    NSString *stringToFind =@"\n";
    entrys = [content componentsSeparatedByString:stringToFind];

    }


    /* Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;*/
}

- (void)viewDidUnload{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    int weeks = 1+1;

    return weeks;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    NSString *sectionHeader = nil;

    if ( section == 0 ) {

       sectionHeader = @"Refresh - test";

    }

    if (  section == 1 ) {
        if ( [Globals sharedGlobals].selectedProject != nil){

            NSString* temp = @"Current in project ";
            [temp stringByAppendingString:[Globals sharedGlobals].selectedProject];

            sectionHeader = temp;

        }

        else {
            sectionHeader = @"No project selected";
        }
    }

    if (  section == 2 ) {
        sectionHeader = @"Week 3";
    }

    if (  section == 3 ) {
        sectionHeader = @"Week 4";
    }

    if (  section == 4 ) {
        sectionHeader = @"Week 5";
    }

    return sectionHeader;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rows;

    if (section == 0){
        rows = 1;
    }

    if (section == 1 ){
        rows = [entrys count] -1;
    }



    return rows ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *oneLine = [entrys objectAtIndex:indexPath.row];
    NSArray *lineComponents = [oneLine componentsSeparatedByString:@";"];

    cell.textLabel.text = [lineComponents objectAtIndex:0];
    cell.textLabel.textColor = [UIColor colorWithRed:0.0 green:0.8 blue:0.2 alpha:1.0];

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{



    if (indexPath.section == 0) {

        [self.tableView reloadData];

        [tableView deselectRowAtIndexPath:indexPath animated:YES];



    }






    if (indexPath.section == 1) {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    SecondOverViewController *anotherViewController = [[SecondOverViewController alloc] initWithStyle:UITableViewStylePlain];

    NSArray *rowArray = [[entrys objectAtIndex:indexPath.row] componentsSeparatedByString:@";"];

    anotherViewController.oneRow = rowArray;

    [self.navigationController pushViewController:anotherViewController animated:YES];

    } 





    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */

}

@end

Thanks in advance:)

+3
source share
1 answer

If your ViewController is derived from a UITableViewController, try calling the reloadData function as follows:

[self.tableView reloadData];

In addition, I recommend calling reloadData under viewDidLoad instead of viewDidAppear . The latter can be called several times unexpectedly (for example, when another view pops out of the stack), and the former is called only once.

For instance:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView reloadData];
}
+1

All Articles