我想在一个中显示一个人的列表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
提前谢谢你的帮助。