对象不会添加到我的NSMutableArray中

时间:2013-11-11 17:39:30

标签: ios objective-c nsmutablearray

我想在UITableView中显示人物列表(我在ViewController中写下名称并将其显示为UITableViewController中的列表)。 我只需要存储数据。但是我的代码只在NSMutableArray中添加了一个对象。 是因为我在班级“顾客”中使用单身人士吗? 到目前为止,这是我的代码:

Customers.h

#import <Foundation/Foundation.h>

@interface Customers : NSObject
{
    NSString *name;
    float age;
}


- (id)initWithName:(NSString *)aname age:(float)aage ;

- (NSString*) name;
- (float) age;

- (void) setName:(NSString*) newName;
- (void) setAge:(float) newAge;

+(Customers*)instance;

@end

Customers.m

#import "Customers.h"

@implementation Customers

- (id)initWithName:(NSString *)aname age:(float)aage {
    if ((self = [super init]))

    {
        self.name = aname;
        age = aage;
    }
    return self;

}

- (NSString*) name{
    return name;
}
- (float) age{
    return age;
}

- (void) setName:(NSString*) newName
{
    name = newName;
}
- (void) setAge:(float) newAge{
    age = newAge;
}

+(Customers*)instance{
    static dispatch_once_t once;
    static Customers *sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] initWithName:@"jean" age:24];
    });
    return sharedInstance;

}
@end

ViewController.h

#import <UIKit/UIKit.h>

@class Customers;

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *nameLabel;
@property (strong, nonatomic) IBOutlet UITextField *ageLabel;
- (IBAction)goToTableView:(id)sender;

@end

ViewController.m

#import "ViewController.h"
#import "Customers.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goToTableView:(id)sender {
    [[Customers instance] setName:_nameLabel.text];    
}
@end

TableViewController.h

#import <UIKit/UIKit.h>

@class Customers;

@interface TableViewController : UITableViewController
{
        NSMutableArray *peopleListe;

}
@end

TableViewController.m

#import "TableViewController.h"
#import "Customers.h"


@interface TableViewController ()

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    Customers *koko = [[Customers alloc ] initWithName:[[Customers instance]name] age:[[Customers instance]age]];
    peopleListe = [NSMutableArray arrayWithObjects: nil];
    [peopleListe addObject: koko];
    NSLog(@"%@",peopleListe);

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return peopleListe.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"MyBasicCell"];
    Customers *list = [peopleListe objectAtIndex:indexPath.row];
    cell.textLabel.text = list.name;

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

@end

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

根据您的评论,您希望每次加载控制器时都添加一个对象。

- (void)viewDidLoad {
    [super viewDidLoad];

    Customers *koko = [[Customers alloc ] initWithName:[[Customers instance] name]
                                                   age:[[Customers instance] age]];

    peopleListe = [NSMutableArray arrayWithObjects: nil];
    [peopleListe addObject: koko];
    NSLog(@"%@",peopleListe);
}

我发现有很多问题:

  1. 我不确定viewDidLoad是否正确嵌入此方法,如果视图已经在内存中,我认为不能保证调用它。 (我没有任何ios经验,所以我不确定。)甚至可能是你的控制器每次使用时都会从头开始删除和创建。

  2. 分配peopleList时替换整个列表。你应该检查它的存在,如果它还不存在,只分配它。 (Obj-C将实例变量初始化为nil,因此只需进行简单的检查即可。)

  3. 除此之外,您似乎混合了控制器和型号代码。如果你有时间,你应该考虑将数组移动到模型和控制器代码之外。