2012-04-07 134 views
0

我用下面的方法來呈現一個名爲DictAddSubSecondCell的UIViewController後重裝的tableView:如何解僱一個UIViewController

// UIViewControler 1 
- (IBAction)addWord:(id)sender{ 
dictAddSubSecondCellController = [[DictAddSubSecondCell alloc] initWithNibName:@"DictAddSubSecondCell" bundle:nil]; 
[self presentModalViewController:dictAddSubSecondCellController animated:YES]; 
[dictAddSubSecondCellController release]; 
} 

,當我在DictAddSubSecondCell點擊按鈕:

- (IBAction)dismissAction:(id)sender{ 
[self dismissModalViewControllerAnimated:YES]; 
} 

中的tableView在UIViewControler 1沒有重新加載它的數據,但我在viewWillApear方法中添加了一個方法:

[self.table reloadData]; 

但它仍然不起作用。我不知道這件事。對我有什麼建議嗎?謝謝。

這裏的UIViewController 1的全部源代碼:

// 
// DictAddSubSecond.m 
// 
// Created by Samuel Armstrong on 4/8/12. 
// Copyright 2012 __MyCompanyName__. All rights reserved. 
// 

#import "DictAddSubSecond.h" 
#import "DictionaryList.h" 

@implementation DictAddSubSecond 
@synthesize dictList, table, editButton; 
@synthesize dictAddSubSecondCellController; 

- (IBAction)addWord:(id)sender 
{ 
    dictAddSubSecondCellController = [[DictAddSubSecondCell alloc] initWithNibName:@"DictAddSubSecondCell" bundle:nil]; 
    [self presentModalViewController:dictAddSubSecondCellController animated:YES]; 
    [dictAddSubSecondCellController release]; 
} 

- (IBAction)toggleEdit { 
    [self.table setEditing:!self.table.editing animated:YES]; 

    if (self.table.editing) { 
     [editButton setTitle:@"Done"]; 
     [editButton setStyle:UIBarButtonItemStyleDone]; 
    } 
    else { 
     [editButton setTitle:@"Edit"]; 
     [editButton setStyle:UIBarButtonItemStyleBordered]; 

    } 
} 

- (IBAction)dismissAction:(id)sender 
{ 
    [self dismissModalViewControllerAnimated:NO]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)refreshTableData 
{ 
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *file = [path stringByAppendingPathComponent:@"dictWithWords.tmp"]; 
    if (dictList == nil) { 
     NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:file]; 
     if ([array objectAtIndex:0] != nil) { 
      dictList = [[NSMutableArray alloc] initWithCapacity:1]; 
      self.dictList = array; 
      [array release]; 
     } 
     else 
     { 
      dictList = [[NSMutableArray alloc] initWithCapacity:1]; 
     } 
    } 
    [self.table reloadData]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self refreshTableData]; 
} 

- (void)someMethodToReloadTable:(NSNotification *)notification 
{ 
    [table reloadData]; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethodToReloadTable) name:@"reloadTable" object:nil]; 


} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadTable" object:nil]; 


} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 




#pragma mark - 
#pragma mark Table View Data Source Methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dictList count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MoveMeCellIdentifier = @"MoveMeCellIdentifier"; 
    UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:MoveMeCellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MoveMeCellIdentifier] autorelease]; 

     //A Boolean value that determines whether the cell shows the reordering control. 
     cell.showsReorderControl = YES; 

    } 
    NSUInteger row = [indexPath row]; 

    cell.textLabel.text = [dictList objectAtIndex:row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 

#pragma mark - 
#pragma mark Table View Delegate Methods 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 
    [self.dictList removeObjectAtIndex:row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
        withRowAnimation:UITableViewRowAnimationMiddle]; 
} 


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 




// Asks the data source whether a given row can be moved to another location in the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 


//Tells the data source to move a row at a specific location in the table view to another location. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    NSUInteger fromRow = [fromIndexPath row]; 
    NSUInteger toRow = [toIndexPath row]; 

    id object = [[dictList objectAtIndex:fromRow] retain]; 
    [dictList removeObjectAtIndex:fromRow]; 
    [dictList insertObject:object atIndex:toRow]; 
    [object release]; 
} 


@end 
+0

邏輯似乎是合理的。你可以使用日誌消息或調試器來檢查兩件事情:實際上是否調用了[[self.table reloadData]],如果是,那麼你是否會在調用之後發現任何調用(例如'numberOfRowsInSection: ')? – 2012-04-07 16:18:38

+0

我在DictAddSubSecondCell中的Documents /文件夾中修改了一個文件(一個使用writeTOfile方法的NSArray實例),並且我想在UIViewControler 1中顯示修改後的結果。 我現在只能通過重載UIViewControler 1來看到新的結果。 – Samblg 2012-04-07 17:35:06

+0

這意味着TableView無法在modalview解除後重新加載 – Samblg 2012-04-07 17:39:34

回答

1

嘗試稱其爲viewDidAppear:,而不是viewWillAppear:

+0

這是不會工作.. – Samblg 2012-04-07 17:33:48

+0

你確定viewDidAppear:被調用嗎?在它內部放置一個NSLog語句來確保。輸入委託方法很容易,必須準確地被調用。 – 2012-04-07 17:36:46

+0

- (void)viewWillAppear:(BOOL)當我解散時調用動畫。 – Samblg 2012-04-07 17:45:29

相關問題