9UITableView和UITableViewController
+(CakesStore *)sharedStore{
static CakesStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];//sharedStore方法掉用NSObject的allocWithZone:
}
return sharedStore;
}
这段代码将sharedStore指针声明为了静态变量static variable。与局部变量不同,程序不会将静态变量保存在栈中,所以当某个定义了静态变量的方法返回时,程序不会释放相应的变量。也就是说程序只会定义某个静态变量一次(当系统将程序载入内存时),并且永远不会释放该变量。与局部变量相同的是,程序只能在声明某个静态变量的代码段中访问该变量。(以上代码除了sharedStore方法外,其他任何对象或方法不能访问sharedStore指针)。
alloc其实是一个“中转”方法,实际调用的是allocWithZone:。allocWithZone:方法又会掉用名为NSAllocateObject的C函数,并由该函数为对象分配内存。
注意:这里只能在CakesStore的方法中跳过CakesStore已经实现的方法。这是因为向super发送消息,其实是向self发送消息,但是要求系统在查找方法时,跳过当前对象的类,从父类开始查询。
@class的使用,当某个头文件发生变化时,对那些通过@class指令导入该文件的其他文件,编译器可以不用重新编译,这样就可以大幅节省编译时间。
- (instancetype)init {
self = [super init];
if (self) {
allCakes = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)allCakes {
return allCakes;
}
- (Cake *)createCake {
Cake *c = [Cake randomCake];
[allCakes addObject:c];
return c;
}
实现数据源方法Required
- tableView:numberOfRowsInSection:
- tableView:cellForRowAtIndexPath:
UITableViewCell对象
UITableViewCell对象有一个子视图:contentView。contentView也包含很多子视图,它的子视图构成UITableViewCell对象的主要外观。此外,UITableViewCell对象还可以显示一个辅助指示视图accessory indicator。辅助指示视图的作用是显示一个指定的图标,用于向用户提示UITableViewCell对象可以执行的动作。这些图标包括勾选标记、展开图标或中间有V型图案的蓝色圆点,辅助指示视图的默认类型是UITableViewCellAccessoryNone。
负责显示UITableViewCell对象所代表的数据,是contentView所包含的三个子视图。textLabel属性和detailTextLabel属性和imageView属性所指向的对象。
UITableViewCellStyleDefault,//左右图+textLabel
UITableViewCellStyleValue1,//左右 textLabel大+detailTextLabel小
UITableViewCellStyleValue2,//左右 textLabel小+detailTextLabel大
UITableViewCellStyleSubtitle//三项
代码片段库
<#代码补全占位符code completion placeholder#>

浙公网安备 33010602011771号