问题
I have UITextView in ViewController. Im passing the textView to viewcontroller1 UITableView. I used NSUserDefaults for store and retrieve text. In viewcontroller1 UITebleView is not increasing with updating text, it replacing old text in only first row.
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;
}
回答1:
declare your array object in your Appdelegate.h
@property (nonatomic, retain) NSMutableArray *textArray;
allocate this array object in your Appdelegate.m
in didFinishLaunchingWithOptions
method
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
if(array)
{
self.textArray = array;
}
else
{
self.textArray=[[NSMutableArray alloc]init];
}
import Appdelegate.h
file in ViewController1.h
and declare this
AppDelegate *appdelegate;
change following method in your code in ViewController1.m
file
- (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];
}
}
change following 2 methods
- (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;
}
回答2:
I understand your requirement. You want to add objects to an existing array when the viewWillAppear. But you are initializing the array whenever viewWillAppear invoked. Thats why you get only one object at every time. For this purpose you have to maintain a singleton class and add object to the array in viewWillAppear.
You have to take a look on this:
http://www.galloway.me.uk/tutorials/singleton-classes/
回答3:
Just alloc yourarray and table in ViewDidLoad
rather then ViewWillAppear
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStylePlain];
textArray=[[NSMutableArray alloc]init];
来源:https://stackoverflow.com/questions/19217085/uitableview-count-not-increasing