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

本文详细介绍了如何使用Item模型创建TableView中的BaseItem和ArrowItem,包括属性定义、创建方法以及如何在实际应用中使用这些模型。通过简单的实例代码演示,帮助开发者快速理解并应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不知不觉过了这么多天了,作者真的是忙的透不过气了,新应用猪猪刚放上 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,但是这个应用还在审核中,所以搞了个内测版让大家先尝尝鲜,等正式出来了,各位就可以下载了,完全个人独立开发的项目喔,么么哒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值