2012-05-01 108 views
0

我有一個帶有根和第二個視圖的navigationcontroller。第二個視圖包含一個pickerview,用戶可以在其中設置0到9的值。我將此值保存在nsuserdefaults 中,並將其用於rootviev tableview中的numberofrowsinsection。 我從userdefaults中檢索pickervalue並將其設置爲NSInteger。 NSLog向​​我顯示正確的值,但作爲表的返回值 - >沒有行。桌面視圖工作,返回值(例如)4顯示我4行...TableView不顯示行

對我有什麼想法?

這裏rootview:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     return retValv ; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath 
{ 
UITableViewCell *cell; 
cell = [UITableViewCell alloc]; 
cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 
cell.textLabel.text = @"Test"; 
return cell; 
} 


-(IBAction)switch1:(id)sender { 
secondView *second=[[secondView alloc]initWithNibName:@"secondView" bundle:nil]; 


[self.navigationController pushViewController:second animated:YES]; 

} 

-(IBAction)showLog:(id)sender 
{ 
NSLog(@"retValv is dzt. %i",retValv); 

} 

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

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
retValv = [defaults integerForKey:@"myInt"]; 
} 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
self.title = @"Locations"; 
} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

和secondview:

#import "secondView.h" 


@interface secondView() 

@end 

@implementation secondView 


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
{ 
return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:  (NSInteger)component 
{ 
return 10; 
} 
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component 
{ 
return [NSString stringWithFormat:@"%i",row]; 
} 
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component 
{ 
NSLog(@"Gewählt: Zeile %i",row); 

zeile = row; 


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setInteger:row forKey:@"pickerRow"]; 
[defaults setInteger:zeile forKey:@"myInt"]; 
[defaults synchronize]; 

} 

-(IBAction)showInt:(id)sender { 

NSLog(@"show value %i",zeile); 

    } 


- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 

self.title = @"Settings"; 


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[picker selectRow:[defaults integerForKey:@"pickerRow"] inComponent:0 animated:NO]; 
zeile = [defaults integerForKey:@"myInt"]; 


} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 
+0

thx!完美的作品! – HugoBoss

+0

你的桌子如何被告知有人使用了選取器?我希望在'pickerView:didSelectRow:'中發出一些信號來表示重新加載。 –

+0

另一個問題:是否有一種簡單的方法來重新加載tableview從第二個切換回firstview後? – HugoBoss

回答

0

在根視圖中移動代碼發送到你的負荷viewWillAppear中,而不是viewDidAppear。它加載數字的時間太晚(在tableview被加載並顯示之後)。