我在ViewController中有UITextView。我将textView传递给viewcontroller1 UITableView。我使用NSUserDefaults存储和检索文本。在viewcontroller1中,UITebleView不会随着更新文本而增加,它只替换第一行中的旧文本。
的viewController:
-(void)save:(id)sender{
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:textView.text forKey:@"savetext"];
[userData1 synchronize];
}
ViewController1:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
textArray=[[NSMutableArray alloc]init];
txt=[[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedValue = [prefs stringForKey:@"savetext"];
txt.text = [NSString stringWithFormat:@"%@", savedValue];
MyAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// [MyAppDelegate.textArray addObject:txt.text];
if(![MyAppDelegate.textArray containsObject:txt.text]){
[MyAppDelegate.textArray addObject:txt.text];
}
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:MyAppDelegate.textArray forKey:@"save"];
[userData1 synchronize];
[self.view addSubview:txt];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// NSLog(@"textArray count is %d",[MyAppDelegate.textArray count]);
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
return [myMutableArrayAgain count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [myMutableArrayAgain objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
答案 0 :(得分:2)
在Appdelegate.h
@property (nonatomic, retain) NSMutableArray *textArray;
在Appdelegate.m
方法
didFinishLaunchingWithOptions
中分配此数组对象
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
if(array)
{
self.textArray = array;
}
else
{
self.textArray=[[NSMutableArray alloc]init];
}
在Appdelegate.h
中导入ViewController1.h
文件并声明此
AppDelegate *appdelegate;
在ViewController1.m
文件
- (void)viewDidLoad
{
[super viewDidLoad];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStylePlain];
NSLog(@"Scrolling");
tableView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleRightMargin;
// tableView.contentInset = UIEdgeInsetsMake(0, 0,300, 0); //values passed are - top, left, bottom, right
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
tableView.contentInset = UIEdgeInsetsMake(0, 0,300, 0);
//self.view = tableView;
[self.view addSubview:tableView];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedValue = [prefs stringForKey:@"savetext"];
txt.text = [NSString stringWithFormat:@"%@", savedValue];
appdelegate = [[UIApplication sharedApplication] delegate];
if(![appdelegate.textArray containsObject:savedValue]){
[appdelegate.textArray addObject:savedValue];
}
}
更改以下两种方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appdelegate.textArray count];
NSLog(@"textArray count is %d",[textArray count]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [appdelegate.textArray objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
答案 1 :(得分:0)
我理解你的要求。您希望在viewWillAppear时将对象添加到现有数组。但是,只要调用 viewWillAppear ,就会初始化数组。这就是为什么你每次只得到一个物体。为此,您必须维护一个单例类并在viewWillAppear中向该数组添加对象。
你必须看看这个:
答案 2 :(得分:0)
只需在ViewDidLoad
而不是ViewWillAppear
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStylePlain];
textArray=[[NSMutableArray alloc]init];