Reputation: 107
I'm trying to to call the [MyTableviewController.tableview reloaddata]
function for my UITableView. I figured the best place to do so is the -(void) viewWillAppear
method of the UITableViewController subclass.
The code somehow wasn't created upon creating the class and i tried to implement the method with little success.
After some research on SO and various other Websites i found hints that the problem might be that the UIViewController subclass is part of an Navigation Controller, which is in turn part of an Tab Bar Controller. The general suggestion and code posted was to subclass one (which?) of the controllers and implement the -viewWillAppear
message.
My questions are: 1. Is there a way to call this much needed method WITHOUT subclassing another controller? 2.If so, how do I do it? 3.If not, could you please explain to me exactly what i have to do and, more importantly, why i have to do it?
Heres the complete Code of the UITableViewController:
//
// 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 :)
Upvotes: 3
Views: 5430
Reputation: 4977
If your ViewController is derived from UITableViewController, try calling the reloadData function this way:
[self.tableView reloadData];
Also, I recommend calling reloadData under viewDidLoad instead of viewDidAppear. The latter can be called multiple times unexpectedly (for example when another view is popped off the stack), while the former is called just once.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView reloadData];
}
Upvotes: 1