#import "JXItemsViewController.h"
@interface JXItemsViewController ()
@end
@implementation JXItemsViewController
- (instancetype)init {
// 调用父类的指定初始化方法
self = [super initWithStyle:UITableViewStylePlain];
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
return [self init];
}
@end
#import "AppDelegate.h"
#import "JXItemsViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 添加初始化代码
// 创建 JXItemsViewController 对象
JXItemsViewController * itemsViewController = [[JXItemsViewController alloc] init];
// 将 JXItemsViewController 的标示图加入窗口
self.window.rootViewController = itemsViewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import <Foundation/Foundation.h>
@interface JXItem : NSObject
/** 创建日期 */
@property (nonatomic,strong,readonly) NSDate * createDate;
/** 名称 */
@property (nonatomic,strong) NSString * itemName;
/** 编号 */
@property (nonatomic,strong) NSString * serialnumber;
/** 价值 */
@property (nonatomic,assign) NSInteger valueInDollars;
/** JXImageStore中的键 */
@property (nonatomic,strong) NSString * itemKey;
+ (instancetype)randomItem;
/**
* JXItem类指定的初始化方法
* @return 类对象
*/
- (instancetype)initWithItemName:(NSString *)name
valueInDollars:(NSInteger)value
serialNumber:(NSString *)sNumber;
- (instancetype)initWithItemName:(NSString *)name;
@end
#import "JXItem.h"
@implementation JXItem
+ (instancetype)randomItem {
// 创建不可变数组对象,包含三个形容词
NSArray * randomAdjectiveList = @[
@"Fluffy",
@"Rusty",
@"Shiny"
];
// 创建不可变数组对象,包含三个名词
NSArray * randomNounList = @[
@"Bear",
@"Spork",
@"Mac"
];
// 根据数组对象所含的对象的个数,得到随机索引
// 注意:运算符%是模运算符,运算后得到的是余数
NSInteger adjectiveIndex = arc4random() % randomAdjectiveList.count;
NSInteger nounIndex = arc4random() % randomNounList.count;
// 注意,类型为NSInteger 的变量不是对象
NSString * randomName = [NSString stringWithFormat:@"%@ %@",randomAdjectiveList[adjectiveIndex],randomNounList[nounIndex]];
NSInteger randomValue = arc4random_uniform(100);
NSString * randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c",
'0' + arc4random_uniform(10),
'A' + arc4random_uniform(26),
'0' + arc4random_uniform(10),
'A' + arc4random_uniform(26)];
JXItem * newItem = [[self alloc] initWithItemName:randomName
valueInDollars:randomValue
serialNumber:randomSerialNumber];
return newItem;
}
- (NSString *)description {
NSString * descriptionString = [NSString stringWithFormat:@"%@ (%@):Worth $%zd, recorded on %@",self.itemName,self.serialnumber,self.valueInDollars,self.createDate];
return descriptionString;
}
- (instancetype)initWithItemName:(NSString *)name
valueInDollars:(NSInteger)value
serialNumber:(NSString *)sNumber {
// 调用父类的指定初始化方法
self = [super init];
// 父类的指定初始化方法是否成功创建了对象
if (self) {
// 为实例变量设置初始值
_itemName = name;
_valueInDollars = value;
_serialnumber = sNumber;
// 设置_createDate为当前时间
_createDate = [NSDate date];
// 创建一个 NSUUID 对象
NSUUID * uuid = [[NSUUID alloc] init];
NSString * key = [uuid UUIDString];
_itemKey = key;
}
// 返回初始化后的对象的新地址
return self;
}
- (instancetype)initWithItemName:(NSString *)name {
return [self initWithItemName:name valueInDollars:0 serialNumber:@""];
}
- (instancetype)init {
return [self initWithItemName:@"Item"];
}
- (void)dealloc {
NSLog(@"Destoryed:%@",self);
}
@end
#import <Foundation/Foundation.h>
@interface JXItemStore : NSObject
// 注意,这是一个类方法,前缀是+
+ (instancetype)sharedStore;
@end
在 JXItemStore 类收到 sharedStore 消息后,会检查自己是否已经创建 JXItemStore 的单例对象。如果已经创建,就返回自己已经创建的对象,否则就需要先创建,然后再返回。
#import "JXItemStore.h"
@implementation JXItemStore
// 单粒对象
+ (instancetype)sharedStore {
static JXItemStore * sharedStore = nil;
// 判断是否需要创建一个 sharedStore 对象
if (!sharedStore) {
sharedStore = [[self alloc] init];
}
return sharedStore;
}
@end
#import <Foundation/Foundation.h> @class JXItem; @interface JXItemStore : NSObject /** 存放 JXItem 对象数组 */ @property (nonatomic,readonly) NSArray * allItem; // 注意,这是一个类方法,前缀是+ + (instancetype)sharedStore; - (JXItem *)createItem; @end
#import "JXItemStore.h"
#import "JXItem.h"
@interface JXItemStore ()
/** 可变数组,用来操作 JXItem 对象 */
@property (nonatomic,strong) NSMutableArray * privateItems;
@end
@implementation JXItemStore
// 单粒对象
+ (instancetype)sharedStore {
static JXItemStore * sharedStore = nil;
// 判断是否需要创建一个 sharedStore 对象
if (!sharedStore) {
sharedStore = [[self alloc] init];
}
return sharedStore;
}
- (NSArray *)allItem {
return self.privateItems;
}
#pragma mark - 懒加载
- (NSMutableArray *)privateItems{
if (_privateItems == nil) {
_privateItems = [[NSMutableArray alloc] init];
}
return _privateItems;
}
@end
#import "JXItemStore.h"
#import "JXItem.h"
@interface JXItemStore ()
/** 可变数组,用来操作 JXItem 对象 */
@property (nonatomic,strong) NSMutableArray * privateItems;
@end
@implementation JXItemStore
// 单粒对象
+ (instancetype)sharedStore {
static JXItemStore * sharedStore = nil;
// 判断是否需要创建一个 sharedStore 对象
if (!sharedStore) {
sharedStore = [[self alloc] init];
}
return sharedStore;
}
- (NSArray *)allItem {
return [self.privateItems copy];
}
- (JXItem *)createItem {
JXItem * item = [JXItem randomItem];
[self.privateItems addObject:item];
return item;
}
#pragma mark - 懒加载
- (NSMutableArray *)privateItems{
if (_privateItems == nil) {
_privateItems = [[NSMutableArray alloc] init];
}
return _privateItems;
}
@end
#import "JXItemsViewController.h"
#import "JXItem.h"
#import "JXItemStore.h"
@interface JXItemsViewController ()
@end
@implementation JXItemsViewController
- (instancetype)init {
// 调用父类的指定初始化方法
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (NSInteger i=0; i<5; i++) {
[[JXItemStore sharedStore] createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
return [self init];
}
@end
@required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // Editing // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath: - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // Index - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED; // return list of section titles to display in section index view (e.g. "ABCD...Z#") - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED; // tell table which section corresponds to section title/index (e.g. "B",1)) // Data manipulation - insert and delete support // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; // Data manipulation - reorder / moving support - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; @end
#import "JXItemsViewController.h"
#import "JXItem.h"
#import "JXItemStore.h"
@interface JXItemsViewController ()
@end
@implementation JXItemsViewController
- (instancetype)init {
// 调用父类的指定初始化方法
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (NSInteger i=0; i<5; i++) {
[[JXItemStore sharedStore] createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
return [self init];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[JXItemStore sharedStore] allItem] count];
}
@end
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1, // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2, // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
}; // available in iPhone OS 3.0
#import "JXItemsViewController.h"
#import "JXItem.h"
#import "JXItemStore.h"
@interface JXItemsViewController ()
@end
@implementation JXItemsViewController
- (instancetype)init {
// 调用父类的指定初始化方法
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (NSInteger i=0; i<15; i++) {
[[JXItemStore sharedStore] createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
return [self init];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[JXItemStore sharedStore] allItem] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建 UITableViewCell 对象,风格使用默认风格
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"];
// 获取 allItem 的第 n 个 JXItem 对象
// 然后将该 JXItem 对象的描述信息赋值给 UITableViewCell 对象的 textLabel
// 这里的 n 是该 UITableViewCell 对象所对应的表格索引
NSArray * items = [[JXItemStore sharedStore] allItem];
JXItem * item = items[indexPath.row];
cell.textLabel.text = [item description];
return cell;
}
@end
#import "JXItemsViewController.h"
#import "JXItem.h"
#import "JXItemStore.h"
@interface JXItemsViewController ()
@end
@implementation JXItemsViewController
- (instancetype)init {
// 调用父类的指定初始化方法
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (NSInteger i=0; i<15; i++) {
[[JXItemStore sharedStore] createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
return [self init];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 向控制器注册
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"UITableViewCell"];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[JXItemStore sharedStore] allItem] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建 UITableViewCell 对象,风格使用默认风格
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
forIndexPath:indexPath];
// 获取 allItem 的第 n 个 JXItem 对象
// 然后将该 JXItem 对象的描述信息赋值给 UITableViewCell 对象的 textLabel
// 这里的 n 是该 UITableViewCell 对象所对应的表格索引
NSArray * items = [[JXItemStore sharedStore] allItem];
JXItem * item = items[indexPath.row];
cell.textLabel.text = [item description];
return cell;
}
@end
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有