-2

に人のリストを表示したいUITableView( に名前を書き、 にViewControllerリストとして表示するUITableViewController)。データを溜め込むしかない。しかし、私のコードはNSMutableArray. クラス「Customers」でシングルトンを使用しているためですか?
これまでの私のコードは次のとおりです。

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

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

よろしくお願いいたします。

4

1 に答える 1