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:
#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) {
}
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];
}
}
- (void)viewDidUnload{
[super viewDidUnload];
}
- (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;
}
#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];
}
}
@end
Thanks in advance:)
source
share