UITableView计数不增加 [英] UITableView count not increasing

查看:145
本文介绍了UITableView计数不增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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;

}


推荐答案

数组对象在 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];    
}

import Appdelegate.h 文件 ViewController1.h 并声明此

import Appdelegate.h file in ViewController1.h and declare this

AppDelegate *appdelegate;

在您的代码中更改 ViewController1.m file

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




    }

更改以下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;
    }

这篇关于UITableView计数不增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆