我想通過NSUserDefaults保存數據,並在tableView上查看它,但沒有發生後,我點擊保存按鈕,我停止後,再次運行應用程序我保存的數據它可以在每次覆蓋舊數據時看到它。難道我做錯了什麼?或者我應該使用與NSUserDefaults不同的東西?Xcode:保存與NSUserDefaults和TableView的數據問題
在此先感謝。
-(IBAction)save{
NSUserDefaults *add1 = [NSUserDefaults standardUserDefaults];
[add1 setObject:txt1.text forKey:@"txt1"];
[add1 synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.dataArray = [NSArray arrayWithObjects:[prefs objectForKey:@"txt1"], nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string = [dataArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=string;
return cell;
}
謝謝你的回答。通常我的tableView指針叫做tbl1,我寫了[tbl1 reloadData];我得到reloadData沒有重新加載?請問你能幫幫我嗎 –