代码规范
实例变量
hello.h文件
@interface Hello // 开放的实例变量;非基本数据类型需要添加引用标记,字符串(NSString)类型标记为copy,非字符串类型标记为retain或者strong。 @end
hello.m文件
@interface Hello (private)
// 不开放的实例变量
@end
@implementation Hello
@synthesize example = _example // example作为初始化变量,而_example作为引用变量
- (void)viewDidLoad {
// 初始化实例变量
Object *tmp = [[Object alloc] init];
self.example = tmp;
[tmp release];
// 操作实例变量
[_example message];
}
@end
视图控制器的生命周期方法编写规范
- (void)dealloc {
// 必须调用父类方法;使用引用变量来释放;释放完了以后必须将指针置空。
[super dealloc];
[_example release];
[_example = nil];
}
- (void)loadView {
// 必须调用父类方法;这里主要用于布局视图。
}
- (void)viewDidLoad {
// 初始化数据,比如发送网络请求。
}
- (void)viewDidUnload {
// 释放在loadView和viewDidLoad里初始化的实例变量,同时将指针置空。
// 因为图片浏览应用比较容易发生内存警告,所以比较好的做法是把图片实体释放掉;
// 比如,通常cell包含imageView,在发生内存警告时,可以将imageView的image属性置空。
// 为了使释放的效率更高,可以将置空的代码放置在自动释放池的管理下,如下:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
cell.imageView.image = nil;
[pool release];
}
- (void)viewWillAppear:(BOOL)animated {
//
}
构造网格视图
本应用所有网格都是规则的,因此可以使用3.0框架里的PCButtonCell视图,该视图允许开发者在上面添加若干个图片按钮,我们使用普通的列表就可以构造出一个网格视图。因为未必每个网格的样式都相同,比如说显示的文本、图标和位置不一样,所以基本PCButtonCell只包含一个PCButton。需要使用额外的视图构件比如UILabel或者UIImageView的,可以继承PCButtonCell然后自定义更加丰富的网格。假设每一行显示n个网格,下面是基本的使用方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dataSource count] / n + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"BrowseLocalController";
PCButtonTableCell *cell = (PCButtonTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
NSDictionary *cellConfig = [self.config objectForPath:PCUI_CONTENT_VIEW, PCUI_TABLE_VIEW, PCUI_TABLE_CELL, nil];
NSInteger countPreRow = n;
// 网格的按钮都使用同一个回调方法,可以使用网格的tag来进行区分;
if (!cell) {
cell = [[PCButtonTableCell alloc] initWithConfig:cellConfig countPreRow:countPreRow target:self action:@selector(clickGrid:) style:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell autorelease];
}
// 每次调用这个方法都必须配对调用showControllerFrom:和hideControlFrom:,因为单元格有重用机制,这样做可以保证单元格在重用以后可以显示正确的网格数。
// 如果需要自定义网格,这两个方法也必须被覆盖。子类必须调用父类实现,并实现额外的控制。
[cell showControlFrom:0];
for (NSInteger i=0; i<countPreRow && i<itemCountForThisRow; i++) {
PCButton *button = [cell getButon:i];
NSInteger itemIdx = i + indexPath.row * countPreRow;
[button setUri:small_url defaultImage:[UIImage imageWithKey:@"album@2x.png"]];
button.tag = itemIdx;
UILabel *label = [cell getLabel:i];
label.text = @"content"
}
[cell hideControlFrom:itemCountForThisRow];
return cell;
}
本地项目(LocalItem?):用于抽象用户保存在本地的图片和图集,也就是说一个LocalItem可以是图片,也可以是图集
ItemType:(enum:{PhotoItem?, AlbumItem?})用于区分图片和图集
saveTime:NSString 保存时间
isChosen:BOOL 用于本地浏览进行编辑操作时判断是否选中
- (NSString *)itemId;子类必须实现,返回图集或者图片的id
- (NSString *)cover;子类可选择实现,返回当前item的封面,即小图
- (NSString *)title;子类可选择实现,无论是图集还是图片返回的都是图集标题
本地项目管理器(LocalItemManager?):用于管理本地项目的类
+ (NSArray *)getItems;读取本地项目
+ (NSArray *)getAlbumItems;获取图集项目
+ (void)saveItem:(LocalItem? *)item;保存某个项目到本地,下文会提到保存路径,以图片或者图集的id作为标识符
+ (void)deleteItem:(LocalItem? *)item://删除本地项目
+ (NSArray *)getAllPhotos;合并所有图集和独立图片成单个集合;原用于浏览大图的预览功能,因为该需求被剔除,所以这个函数可以选择实现。
本地项目的保存结构
- documents/
- save/
- classes/
- photoItems/
- albumItems/
图集(Album):
name : NSString 名称
author : NSString 作者
type : NSString 分类
cover : NSString 封面url
total : NSString 图片数量
origin_url : NSString 对应的web链接
theme : NSString 主题
location : NSString 拍摄地点
create_time : NSString 拍摄时间
nature : NSString 拍摄性质
vote : NSString 获票数
abstruct : NSString 作品概况
clicks : NSString 点击数
is_picked : BOOL 是否精华
以上属性组织为一个NSMutableDictionary对象
+ shareByEmailTemplate: NSString email分享的模板
+ shareByWeiboTemplate: NSString 微博分享的模板
图片列表(PhotoList?) : 浏览大图用到的,与图集是不同的概念;因为图片的体积较大,不能同时保存PhotoList里的所有图片实体,需要根据性能和体验调整容量。
count : NSInteger 图片数量
currentIdx : NSInteger 当前浏览的索引
photos : NSArray 用于浏览的图片列表
+ slideShowTimeInterval : CGFloat 这个应该直接读取app设置
- (Photo *)getPhotoByIndex:(NSInteger)idx; 获取某索引的Photo
- (Photo *)releasePhotoAtIndex:(NSInteger)idx; 释放某索引里的Photo的图片实体
图片(Photo):
photo_id : NSString 图片id
album_id : NSString 从属的图集id
album_name : NSString 从属的图集名称
name : NSString 图片名称
big_photo_url : NSString 图片url
small_photo_url : NSString 小图url
is_picked : BOOL 是否精华
save_time : NSString 被保存的时间
ISO : NSString iso
exposure : NSString 曝光
aperture : NSString 光圈
shutter : NSString 快门
ev : NSString 曝光补偿
camera : NSString 相机
以上属性组织为一个NSMutableDictionary对象
image : (UIImage *) 图片实体
- (void)is_saved;
评论(Comment):
user_id : NSString 评论者的太平洋通行证id
name : NSString 评论者名称
content : NSString 评论内容
create_time : NSString 发表时间
- (void)postComment; 发送评论
下载任务(DownloadTask?):
enum {loading, waiting, stopped, finished}State
state : State
save_time: NSString 启动下载的时间
album : Album 图集对象
downloaded_count : NSInteger 下载成功的图片数
total_count : NSInteger 该图集的图片数
failed_count : NSInteger 下载失败的图片数
loading_idx : NSInteger 该索引指向的图片为下载中
conn : NSURLConnection 某图片的链接对象
- (void)start; 启动自己,将状态设置为Loading
- (void)stop; 暂停自己,将状态设置为Stopped
- (void)continue; 继续下载,将状态设置为Loading
- (void)notify; 完成任务时通知,即loading_idx>=total_count时通知
- (void)save; 将自己序列化到磁盘,路径为/home/Documents/save/classes/albumItems/,以albumId为标识符
- (void)deletePhoto:(Photo *)photo; 删除某张图片,删除后需要重新序列化覆盖原来的项目
下载队列(DownloadQueue?):
@private
+ tasks : NSArray 元素为下载任务,下载完成之后将该元素序列化。
+ (void)startTaskAtIndex:(NSInteger)idx;
+ (void)stopTaskAtIndex:(NSInteger)idx;
+ (void)deleteTaskAtIndex:(NSInteger)idx;
+ (void)receiveSuccesNotify:(NSNotification *)noti;
+ (BOOL)isLoading;检查任务列表中是否还有loading状态的任务,但是因为队列中只有loading和waiting的任务,所以可以通过判断[tasks count]是否为0来实现。
+ (void)notifyWhenDone; 是所有任务都下载的时候通知,还是没有等待中任务也通知?
+ (void)addTask:(DownloadTask? *)task;
+ (void)deleteTask:(DownloadTask? *)task; 用户主动删除任务
+ (void)continueDownload;从LocalItemManager中同步下载任务,在询问用户并得到用户的确认后执行该方法。
摄影师(Photographer)
NSDictionary *properties;
关注的摄影师(FocusedPhotographer?):
@private
+ user_ids : NSArray 已关注的摄影师
+ (void)getAllPhotographers; 获取所有已经关注的摄影师
+ (BOOL)haveFocusedPhotographer:(NSString *)user_id; 判断是否已经关注了指定摄影师
+ (void)focusPhotographer:(NSString *)user_id; 关注某摄影师
![(please configure the [header_logo] section in trac.ini)](http://www1.pconline.com.cn/hr/2009/global/images/logo.gif)