IOS——TableView 中利用Item模型进行 Cell 的开发(2)Item 模型篇

来源:互联网 发布:h3c stp 阻塞端口 编辑:程序博客网 时间:2024/06/08 18:24

不知不觉过了这么多天了,作者真的是忙的透不过气了,新应用猪猪刚放上 APPStore,后脚转吧应用有更新大版本,累到在地了。。好了,废话不多说,给大家继续我们的 TableView利用 Item 模型开发的第2编了,相信大家都看过作者的第一篇了,第一篇主要讲父类 TableView 里面的创建,那么 Item 模型怎么创建呢

当然第一步,我们要有一个父类的 Item,也就是上一篇在文章中多次出现的 BaseItem 了

#import <Foundation/Foundation.h>typedef void(^BaseItemOption)();@interface BaseItem : NSObject// 这个属性是用来标志 Item 类型,告诉系统,我们要创建哪一种 cell@property (nonatomic,assign) ItemType itemType;// 这个属性是 block 属性,用来放置一个预防操作的@property (nonatomic,copy) BaseItemOption option; // 因为许许多多的 cell 都会用到标题图片这两个属性,所以特意在父类先添加了@property (nonatomic,copy) NSString *title; // 标题@property (nonatomic,copy) NSString *icon; // 图片 // 用来选择点击样式,如果为 Yes,则cell 的selectionStyle为UITableViewCellSelectionStyleNone@property (nonatomic,assign) BOOL NotEnable;// 用来返回cell的高度@property (nonatomic,assign) CGFloat height; // 工厂方法,快速创建 cell+ (instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon type:(ItemType)type;
#import "BaseItem.h"@implementation BaseItem+ (instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon type:(ItemType)type{// 这里一定要用[[self alloc]init],因为 其子类运用父类方法的时候,self 能识别出子类的真实类型    BaseItem *item = [[self alloc]init];    item.title = [title copy];    item.icon = [icon copy];    item.itemType = type;    return item;}@end

好了,父类的 item 我们已经创建好了,那么,我们现在来个简单的例子,就是我们常见的这种样式了,如下图

这种系统设置的 cell

以下我们就叫这种 cell 为 ArrowItem,因为每点击一个这样的 Item 都会调到不同的控制器。

那么这个 ArrowItem 内部是怎么个神奇呢,能一句代码就创建?那么我们就看看ArrowItem里面究竟有什么东西

#import "BaseItem.h"@interface FSArrowItem : BaseItem// 需要跳转到的控制器的类@property (nonatomic,assign) Class desveClass;/** 是否需要判断用户是否已经登录,如果未登录,则跳到登录界面*/@property (nonatomic,assign) BOOL judge;+ (instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon  desveClass:(Class)desveClass isJudgeLogin:(BOOL )judge itemType:(ItemType)type;@end
#import "FSArrowItem.h"@implementation FSArrowItem+ (instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon  desveClass:(Class)desveClass isJudgeLogin:(BOOL )judge  itemType:(ItemType)type{    FSArrowItem *item = [super itemWithTitle:title icon:icon type:type];    item.desveClass = desveClass;    item.judge = judge;    return item;}@end

哈哈,相信大家也知道,这是个标准的模型了,那么当然是很简单的模型了呀。

到这里,如果有经验的朋友相信结合第一篇的博客就已经大概能猜出作者想要表达的是什么了,那么我们继续说,这么简简单单的一个模型用起来复杂么那么,作者就用项目的一句源代码告诉大家

- (void)buildUI{    FSArrowItem *phtots = [FSArrowItem itemWithTitle:@"相册" icon:@"FS_Profile_Photo" desveClass:[FSDynamicViewController class] isJudgeLogin:YES  itemType:ItemTypeArrow];    BaseItem *collect = [FSArrowItem itemWithTitle:@"收藏" icon:@"FS_Profile_Collection" desveClass:[FSCollectionViewController class] isJudgeLogin:YES itemType:ItemTypeArrow];    self.messageItem = [FSArrowItem itemWithTitle:@"消息" icon:@"FS_Profile_Message" desveClass:[FSProfileMessageViewController class] isJudgeLogin:YES  itemType:ItemTypeArrow];    BaseItem *grade = [FSArrowItem itemWithTitle:@"积分" icon:@"FS_Profile_Grade" desveClass:[FSPointViewController class] isJudgeLogin:YES  itemType:ItemTypeArrow];    BaseItem *system = [FSArrowItem itemWithTitle:@"系统设置" icon:@"FS_Profile_System" desveClass:[FSSystemViewController class] isJudgeLogin:NO  itemType:ItemTypeArrow];    NSArray *group0 = @[phtots,collect,self.messageItem,grade,system];    [self.dataSource addObject:group0];}

好了,就这么简单几句代码就把上图创建好了,没错,就是这么几句代码,说好的一句一个 cell 嘛,那么 cell 的内部要怎么实现呢?这就是我们下一篇博客的内容了,今天就先到这里啦,如果喜欢我们朋友可以关注一下我,明天继续我们的《TableView 中利用Item模型进行 Cell 的开发(3)TableViewCell 篇》

以上图片为新应用猪猪的截图,喜欢的朋友可以来下面这个链接下载http://pre.im/3a15,但是这个应用还在审核中,所以搞了个内测版让大家先尝尝鲜,等正式出来了,各位就可以下载了,完全个人独立开发的项目喔,么么哒

0 0
原创粉丝点击