2010-01-20 26 views
1

嘿,我有這個問題,UITableView Not refreshing after Modal View Controller dismisses如何重新加載表視圖,數據源(陣列)後由模態視圖控制器正在編輯

但是,我沒有似乎得到一個直接的答案,

任何人都可以擺脫一些光線嗎?

繼承人的代碼。

FirstViewController.h

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

NSMutableArray *routines; 
IBOutlet UITableView *myTableView; 

} 

@property (nonatomic, retain) NSMutableArray *routines; 
@property (nonatomic, retain) UITableView *myTableView; 

- (IBAction)showNewEventViewController; 

@end

及其.M

#import "FirstViewController.h" 
#import "NewEventViewController.h" 

@implementation FirstViewController 

@synthesize routines, myTableView; 


- (void)viewWillAppear:(BOOL)animated{ 
[myTableView reloadData]; 
NSLog(@"Routines: %@", routines); 
NSLog(@"refreshed!"); 

for(int i = 0; i < [routines count]; i++){ 

    NSLog(@"%@", [routines objectAtIndex:i]); 

      } 

} 




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [routines count]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Set up the cell... 
NSString *cellValue = [routines objectAtIndex:indexPath.row]; 
[cell.textLabel setText:cellValue]; 

return cell; 
} 



- (IBAction)showNewEventViewController {  

NewEventViewController *controller = [[NewEventViewController alloc] initWithNibName:@"NewEventView" bundle:nil]; 
controller.routines = routines; 
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentModalViewController:controller animated:YES]; 

[controller release]; 
} 




- (void)viewDidLoad { 

routines = [[NSMutableArray alloc] init]; 

[routines addObject:@"Hello"]; 
[routines addObject:@"Temp"]; 
[routines addObject:@"Temp2"]; 
[routines addObject:@"Temp3"]; 
[routines addObject:@"Temp4"]; 


} 

和模態視圖控制器的.h(NewEventViewController.h)

#import <UIKit/UIKit.h> 


@interface NewEventViewController : UIViewController { 

IBOutlet UITextField *RoutineTitle; 

IBOutlet UITextField *RoutineInvolvment; 


NSMutableArray *routines; 


} 


@property(nonatomic, retain) NSMutableArray *routines; 


-(IBAction)done; 


@end 

和它的.h

#import "NewEventViewController.h" 
#import "FirstViewController.h" 




@implementation NewEventViewController 
@synthesize routines; 


-(IBAction)done{ 



[routines addObject:RoutineTitle.text]; 
[self dismissModalViewControllerAnimated:YES]; 
NSLog(@"Routines: %@", routines); 

} 

回答

0

你一直在問相同的問題,所以顯然在某個地方有斷開連接。發佈您用於展示模態視圖控制器的代碼。工作流應該是這樣的:

  • 創建視圖控制器,你要模態推動。確保它具有用於設置表視圖數據容器的ivar和訪問器(例如setDataContainer:(NSMutableArray *)dataContainer)

  • 在視圖控制器上調用setDataContainer:例程。

  • 目前模態視圖控制器

  • 在你完成按鈕,記錄添加到您的數據容器,您設置([dataContainer ADDOBJECT:NEWOBJECT],並解僱模態視圖控制器

  • 呼叫reloadData在viewWillAppear中,你正在做的從上面的鏈接代碼。

我們不能幫你,如果你不張貼一些代碼,顯示你在做什麼。

最好的問候,

+0

ive添加了代碼。 我從另一個答案的建議中這樣寫道。 所以,如果你看到它的錯誤,那麼請糾正它。 但是,它似乎將字符串添加到數組中,從MVC, 但它不重新加載它。 :( – 2010-01-20 06:27:39

+0

你的代碼看起來很好,我看到的唯一的東西是你的viewDidLoad沒有調用[super viewDidLoad];另外,你的表視圖插座是否斷開連接?這可以解釋爲什麼當你調用reloadData 。我自己創建了一個項目,它可以毫無問題地完成你想要的任務,看看它並比較你所擁有的東西,然後讓我知道它們之間的差別,目前我看不到它:http: //www.cimgf.com/files/CreateModalRecord.zip – 2010-01-20 07:17:48

+0

我改變了我的一些東西,但沒有什麼區別。如果我只是建立在你的項目上,你會感到惱火嗎? – 2010-01-20 08:14:12

0

您完成的選擇器需要調用reloadData。如果一切正確連線(表視圖委託,數據源)它應該正常工作。但是,您可以將視圖設置爲隱藏並重新顯示,直到它可以正常工作,但它太噁心(而不是正確的方法)。

你做的是NewEventViewController的一部分,所以您可以:

1張通行證通過使用NSNotificationCenter defaultCenter

2.Make的FirstViewController實例數據的全局就是從做選擇訪問。

相關問題