我试图为我的UITableView调用[MyTableviewController.tableview reloaddata]
函数。我认为最好的地方是UITableViewController子类的-(void) viewWillAppear
方法。
代码以某种方式在创建类时没有创建,我试图实现该方法但收效甚微。
在对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
提前致谢:)
答案 0 :(得分:1)
如果您的ViewController派生自UITableViewController,请尝试以这种方式调用reloadData函数:
[self.tableView reloadData];
另外,我建议在 viewDidLoad 下调用reloadData,而不是 viewDidAppear 。后者可以被意外地多次调用(例如,当从堆栈弹出另一个视图时),而前者只被调用一次。
例如:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView reloadData];
}