2013-10-07 35 views
0

我在ViewController中有UITextView。我将textView传递给viewcontroller1 UITableView。我使用NSUserDefaults来存储和检索文本。在viewcontroller1中,UITebleView不会随着更新文本而增加,它只会替换第一行中的旧文本。UITableView计数不增加

的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

请输入您从nslog – manujmv

+0

获得的文本数组数,以及将文本存储到NSUserdefaults的代码。 –

+0

我没有在nslog中得到textArray count ... – user2807197

回答

2

声明你的数组对象在Appdelegate.h

@property (nonatomic, retain) NSMutableArray *textArray; 

Appdelegate.mdidFinishLaunchingWithOptions方法分配这个数组对象

NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]]; 
if(array) 
{ 
self.textArray = array; 
} 
else 
{ 
     self.textArray=[[NSMutableArray alloc]init];  
} 

导入Appdelegate.h文件中ViewController1.hViewController1.m文件代码中的声明本

AppDelegate *appdelegate; 

变化如下方法

- (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]; 
     } 




    } 

变化以下2种方法

- (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; 
    } 
0

我理解你的要求。你想在viewWillAppear时将对象添加到现有的数组中。但是,只要调用了viewWillAppear,就会初始化数组。这就是为什么你每次只能得到一个对象。为此,您必须维护一个单例类,并在viewWillAppear中将对象添加到数组中。

你必须对此采取一看:

http://www.galloway.me.uk/tutorials/singleton-classes/

+0

不,它显示相同的东西 – user2807197

+0

@ user2807197:请找到我编辑的答案 – manujmv

0

只是ALLOC yourarray和表ViewDidLoad而不是ViewWillAppear

tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)           style:UITableViewStylePlain]; 

    textArray=[[NSMutableArray alloc]init];