-2

我想在一个中显示一个人的列表UITableView(我在中写下名字ViewController并将它们显示为在中的列表UITableViewController)。我只需要储存数据。但是我的代码只在NSMutableArray. 是因为我在“客户”类中使用了单例吗?
到目前为止,这是我的代码:

客户.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

客户.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

视图控制器.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

视图控制器.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

表视图控制器.h

#import <UIKit/UIKit.h>

@class Customers;

@interface TableViewController : UITableViewController
{
        NSMutableArray *peopleListe;

}
@end

表视图控制器.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

提前谢谢你的帮助。

4

1 回答 1

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,所以一个简单的检查就足够了。)

除此之外,您似乎混合了控制器和模型代码。如果您有时间,您应该考虑将数组移入模型并移出控制器代码。

于 2013-11-11T17:54:25.017 回答