我正在尝试[MyTableviewController.tableview reloaddata]
为我的 UITableView 调用该函数。我认为最好的 -(void) viewWillAppear
方法是 UITableViewController 子类的方法。
不知何故,在创建类时没有创建代码,我试图实现该方法,但收效甚微。
在对 SO 和其他各种网站进行一些研究后,我发现提示问题可能在于 UIViewController 子类是导航控制器的一部分,而导航控制器又是标签栏控制器的一部分。发布的一般建议和代码是将控制器中的一个(哪个?)子类化并实现-viewWillAppear
消息。
我的问题是: 1. 有没有办法在不继承另一个控制器的情况下调用这个急需的方法?2.如果是这样,我该怎么做?3.如果没有,你能否向我解释一下我必须做什么,更重要的是,为什么我必须这样做?
下面是 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
提前致谢 :)