简体   繁体   English

UITableView计数没有增加

[英]UITableView count not increasing

I have UITextView in ViewController. 我在ViewController中有UITextView。 Im passing the textView to viewcontroller1 UITableView. 我将textView传递给viewcontroller1 UITableView。 I used NSUserDefaults for store and retrieve text. 我使用NSUserDefaults来存储和检索文本。 In viewcontroller1 UITebleView is not increasing with updating text, it replacing old text in only first row. 在viewcontroller1中,UITebleView不会随着文本的更新而增加,它仅替换第一行中的旧文本。

viewController: viewController:

-(void)save:(id)sender{

     NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
    [userData1 setObject:textView.text forKey:@"savetext"];
    [userData1 synchronize];
}

ViewController1: 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;

}

declare your array object in your Appdelegate.h Appdelegate.h声明数组对象

@property (nonatomic, retain)  NSMutableArray *textArray;

allocate this array object in your Appdelegate.m in didFinishLaunchingWithOptions method didFinishLaunchingWithOptions方法中的Appdelegate.m中分配此数组对象

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 ViewController1.h导入Appdelegate.h文件并声明

AppDelegate *appdelegate;

change following method in your code in ViewController1.m file 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];
         }




    }

change following 2 methods 更改以下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;
    }

I understand your requirement. 我了解您的要求。 You want to add objects to an existing array when the viewWillAppear. 您想在viewWillAppear时将对象添加到现有数组。 But you are initializing the array whenever viewWillAppear invoked. 但是,只要调用viewWillAppear即可初始化数组。 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. 为此,您必须维护一个singleton类并将对象添加到viewWillAppear中的数组。

You have to take a look on this: 您必须看一下:

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

Just alloc yourarray and table in ViewDidLoad rather then ViewWillAppear 只需在ViewDidLoadViewDidLoad yourarray和表,而不是ViewWillAppear

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

    textArray=[[NSMutableArray alloc]init];    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM