2013-10-07 67 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];