tableViewCell、collectionViewCell、组头组尾等总结

4 篇文章 0 订阅
使用 xib 方式自定义 cell
tableV iew 使用代码方式自定义 cell
coll ectionView 使用代码方式自定义 cell
原型 cell
静态 cell
  •  

使用xib方式自定义cell:
1. 创建一个自定义的 cell 类,并继承UITableViewCell
2. 设置 xib 文件里面的 cell 需要和自定义的 cell 相关联(在类型控制器中更改class)
3. 设置 xib 文件中属性控制器下的可重用 ID ,要和自定义cell中的相同
4. 拖线生成属性
5. cell 定义一个模型属性,并重写该模型属性的set方法
计算机生成了可选文字:+ (instancetype)goodsCellWithTableView: (UI TableView *)tableView { [tableView dequeueReusableCellWithIdentifier:ID] ; HMGoodsCell *cell 1 ce = [ [ [NSBundle mainBundle] owner: nil options:nil] lastObject] ; cell return cell;
 
原型cell的方式自定义cell:
1. 创建一个自定义的 cell 类,并继承UITableViewCell
2. 设置 xib 文件里面的 cell 需要和自定义的 cell 相关联(在类型控制器中更改class)
3. 设置 xib 文件中属性控制器下的可重用 ID ,要和自定义cell中的相同
4. 拖线生成属性
5. cell 定义一个模型属性,并重写该模型属性的set方法
计算机生成了可选文字:(UITableViewCeII *)tableView: (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath * ) indexPath { / / 1. 获 取 模 型 数 据 self apps[indexPath. row HMApp *app / / 2 . 创 建 Cell static NSString *ID = @"app_cell" [tableView dequeueReusableCellWithIdentifier:ID]; HMAppCeII *cell / / 为 每 个 Ce 且 指 定 一 个 代 理 cell.delegate self, / / 3 · 把 模 型 数 据 设 置 给 cell cell. app app; / / 4 · 返 回 Cell return cell; 这 里 只 需 要 在 缓 存 池 中 查 找 相 同 旧 的 ce 丨 丨 就 OK 了 , 如 果 缓 存 池 中 没 有 , 系 统 就 根 据 原 型 c 酬 重 新 创 建 一 个 , 不 需 要 我 们 手 动 创
 
原型cell和xib创建cell的不同点:
xib创建cell比原型cell多一步(在缓存池中没有找到cell,xib方式需要自己手动创建一个cell,而原型cell不需要手动创建,系统会自己根据原型cell的模板来创建)
 
tableV iew 代码方式自定义cell:
1. 创建一个自定义的 cell 类,并继承UITableViewCell
2. 手动在类扩展中定义需要的控件属性
3. 重写 initWithStyle 方法 ,在方法里面创建子控件,并赋值给类扩展中的属性(这里就相当于拖线)
4. cell 定义一个模型属性,并重写该模型属性的set方法
计算机生成了可选文字:#pragma mark - + (instancetype)weiboCellWithTableView: (UI TableView *)tableView { static NSString *ID — @"weibo_cell"; - [tableView dequeueReusableCellWithIdentifier. HMWeiboCell *cell - (cell =- nil) { if [ [HMWeiboCell al loc] reuseldentifier: ID] ; cell - cell; return
 
tableView的团购案例:
xib方式创建单元格
计算机生成了可选文字:> 14 Placeholders File's Owner First Responder goods_cell Views Image View x HMGoodsCell.xib goods_cell Label Label e View Cell Style Identifier Selection Accessory Editing Acc. Indentation Label Custom goods_cell Default None None Level 10 Width O Indent While Editing Shows Re-order Controls Separator Default Insets
 
计算机生成了可选文字:#import <UIKit/UIKit. @class HMGoods; @interface HMGoodsCell . UlTab1eViewCe11 UlTableViewCell @property (nonatomic, strong) HMGoods *goods ; + (instancetype)goodsCellWithTableView: (UI TableView *)tableView; @end
 
计算机生成了可选文字:#pragma mark - + (instancetype)goodsCellWithTableView: (UI TableView *)tableView { static NSString *ID = *cell = [tableView dequeueReusableCellWithIdentifier:ID] ; HMGoodsCell if (cell nil) { = [ [ [NSBundle mainBundle] owner: nil cell options: nil] lastObject]; return cell;
 
xib方式创建footerView(headerView和footerView方式一样):
计算机生成了可选文字:Views HMGoodsFooterView.xib Goods Footer View Placeholders File's Owner First Responder Goods Footer View Custom Class Module Identity Restoration ID HMGoodsFooterView None User Defined Runtime Attributes Key Path Type *RID Document Value Title Item
 
计算机生成了可选文字:#import <UIKit/UIKit. @class HMGoodsFooterView; @protocol HMGoodsFooterViewDelegate <NS0bject> - (void)goodsFooterViewDidClickLoadMoreButton: (HMGoodsFooterView *)footerView; @end UlView @interface HMGoodsFooterView : UIView @property (nonatomic, weak) id<HMGoodsFooterViewDelegate > delegate; + (instancetype)goodsFooterView; @end
 
计算机生成了可选文字:+ (instancetype)goodsFooterView { *footerView = [ [ [NSBundle mainBundle] HMGoodsFooterView loadNibNamed:@"HMGoodsFooterView'I owner: nil options: nil] lastObject] ; return footerView•
 
计算机生成了可选文字:#pragma mark - viewDidLoad - (void)viewDidLoad { [super viewDidLoad] ; self. tableView. rowHeight // jaäEfif00terView HMGoodsFoote rV iew *foote rV iew self. tableView. tableFooterView = footerView; // view HMGoodsHeade rV iew *heade rV iew self. tableView. tableHeaderView = headerView; iMiü#fifiit EheaderView tableView* [HMGoodsFooterView goodsFooterView] ; [HMGoodsHeaderView goodsHeaderView] ;
 
tableView的微博案例(QQ聊天布局案例和它相同):
代码方式创建单元格:
计算机生成了可选文字:#import <UIKit/UIKit. @class HMWeiboFrame; @interface HMWeiboCell . UlTab1eViewCe11 UlTaDleViewCell @property (nonatomic, strong) HMWeiboFrame *weiboFrame; + (instancetype)weiboCellWithTableView: (UI TableView *)tableView; @end
 
计算机生成了可选文字:#pragma mark - + (instancetype)weiboCellWithTableView: (UI TableView *)tableView { static NSString *ID = *cell = [tableView dequeueReusableCellWithIdentifier:ID] ; HMWeiboCell if (cell nil) { [ [HMWeiboCell al loc] initWithStyle: ITableViewCellStyleDefault cell reuseldentifier: ID] ; return cell; #pragma mark - iMjä initWithStyIe - (instancetype) initWithStyle: (UI TableViewCellStyle)style reuseldentifier: (NSString reuseldentifier { 1 se super Inl 1 y e:s y e reuse UllmageView *imgViewIcon = [ [UllmageView en 1 ler: reuse en 1 ler al loc] init] ;
 
tableView的QQ好友列表案例:
代码方式创建组头、组尾:
计算机生成了可选文字:#import <UIKit/UIKit. @class HMGroup; @interface HMGroupHeaderView : UI TableViewHeaderFooterView @property (nonatomic, strong) HMGroup *g roup ; + (instancetype)groupHeaderViewWithTableView: (UI Tabl @end
 
计算机生成了可选文字:+ (instancetype)groupHeaderViewWithTableView: (UI TableView *)tableView { static NSString *ID = @"group_header_view"; *headerVw = [tableView HMG roupHeade rV iew dequeueReusableHeaderFooterViewWithIden inltWithReuseI if (headerVw nil) { // initWithReuseIdentifier headerVw = [[HMGroupHeaderView alloc] initWithReuseIdentifier: D] ; return headerVw; // initWithReuseIdentifier - (instancetype) initWithReuseIdentifier: (NSString reuseldentifier { [super initWithReuseIdentifier: reuseldentifier]) { if (self - // header
 
计算机生成了可选文字:// äEXGÆfiheader - (UIView *)tableView: (UI TableView *)tableVie viewForHeaderInSection: section { i*tabIeView = self.groups[section] ; HMGroup roup // 2. view NSInteger) *headerVw = [HMGroupHeaderView groupHeaderViewWithTableView: HMG roupHeade rV iew tableView] ; // header view // fEZ: GheaderVw, Gtable view // ißLGtable view2äjåN}Etable viewä9fiiQN, viewfif rame // FfiL\Å, view —FEfiframeÆO, header viewfio heade rVw. group = group; // 4. jfi@header view return headerVw;
 
tableView的应用管理案例:
原型cell方式创建单元格:
计算机生成了可选文字:Views M...rd Table View Controller Scene Table View Controller v app_cell app_cell < Table View ontent View First Respon Exit Storyboard Entry Point Prototype Cells Label Image Label w Cell Style Custom Identifier app_cell Accessory None Editing Acc. None Indentation Level Width O Indent While Editing 000 10 Table View Prototype Content
 
计算机生成了可选文字:#import <UIKit/UIKit.h> @class HMApp, HMAppCell; @protocol HMAppCellDelegate 4SObject> - (void)appCellDidClickDownloadButton: (HMAppCell appCell; @end @interface HMAppCe11 . @property (nonatomic, @property (nonatomic, @end UlTableViewCell UlTab1eViewCe11 weak) id<HMAppCellDelegate > delegate; strong) HMApp *app;
 
计算机生成了可选文字:- (UI TableViewCell *)tableView: (UI TableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *ID = = [tableView dequeueReusableCellWithIdentifier:ID] ; HMAppCe11 *cell cell. delegate = self; cell. app - app; // 4. jÉ@Cell return cell;
 
 
collectionView的案例:
纯代码自定义cell:
计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMAppCollectionViewCell . UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end
 
计算机生成了可选文字:// initWithFrame - (instancetype) initWithFrame: (CGRect)frame { [super initWithFrame:frame]) { if (self - = [ [UllmageView al loc] UllmageView *imgViewIcon [self. contentView addSubview: imgViewIcon] ; self . imgViewIcon imgV iewlcon ; // label = [[UILabel al loc] init] ; UILabel *lblName lblName. font = [UIFont systemFont0fSize: 12] ; lblName. textAlignment = NSTextAlignmentCenter; [self. contentView addSubview: lblName] ; self . lblName = lblName; // button = [[UIButton al loc] init] ; UIButton *button init] ; [button setBackgroundImage: [UI Image imageNamed:@Ibuttongreen"] forState: UICont rolStateNormal] ;
 
下面这行代码会执行 initWithFrame 方法
HMAppCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier : ID forIndexPath :indexPath];
计算机生成了可选文字:static NSString * const ID - void viewDidLoad [super viewDidLoad] ; [self. collectionView registerClass: [HMAppCollectionViewCell class forCellWithReuseIdent1f1er // iQNCollection = [UIColor whiteColor] ; self. collectionView. backg roundColor - (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath { = self. apps [indexPath. item] ; *model HMApp cell HMAppCol lectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jä@cell return cell;
 
xib方式自定义cell:
计算机生成了可选文字:> 14 Placeholders File's Owner First Responder app_cell Img Vie on Lbl Name @Btn Download Constraints Views HMAppCollectionViewCell.xib äEXficeII Ima Label app_cell eusable View Identifier app_cell View Mode Center Semantic Unspecified Tag Interaction O User Interaction Enabled O Multiple Touch Alpha Background Default Tint Default 1 Drawing Opaque Hidden O Clears Graphics Context O Clip Subviews O Autoresize Subviews Stretching x 1 1
 
计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMAppCollectionViewCell . UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end
 
计算机生成了可选文字:static NSString * const ID - (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath {l = self. apps [indexPath. item] ; *model HMApp // 2. @lJECe11 HMAppCol lect ionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jä@cell return cell; #pragma mark - viewDidLoad - (void)viewDidLoad { [super viewDidLoad] ; iMiä registerNib UINib *nib = [UINib nibWithNibName:@"HMAppCollectionViewCell" bundle: nil] ; [self. collectionView registerNib: nib forCellWithReuseIdentifier: = [UIColor whiteColor] ; self. collectionView. backg roundColor
 
原型cell方式自定义cell:
计算机生成了可选文字:> 14 ä EXCell Collection View app_cell O View Controller Scene Collection View app_cell Colle Vie... First Respon er Exit Storyboard Entry Poi... Collection View Contr... View Controller Scene > coil View Ima Label Identifier app_cell Mode Center Semantic Unspecified Tag Interaction O User Interaction Enabled O Multiple Touch Alpha Background Default Tint Default 1 Drawing Opaque Hidden O Clears Graphics Context O Clip Subviews O Autoresize Subviews Stretching x 1 1
 
计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMCollectionViewCell UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end
 
计算机生成了可选文字:- (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath { = self. apps [indexPath. item] ; *model HMApp // (prototype cell) static NSString *ID = = [collectionView HMCollectionV iewCell *cell dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jÉ@Cell return cell;
 
代码方式创建组头、组尾:
计算机生成了可选文字:#import <UIKit/UIKit. @class HMPhoto; // UICollectionReusa @interface HMSectionHeaderView : UICollectionReusableView @property (nonatomic, strong) HMPhoto *photo ; @end
 
计算机生成了可选文字:- (instancetype) initWithFrame: (CGRect)frame { [super initWithFrame:frame]) { if (self = [[UILabel al loc] init] ; UILabel *lblTitle lblTitle. font = [UIFont systemFontOfSize: 12] ; lblTitle. textAlignment = NSTextAlignmentCenter; [self addSubview: lblTitle] ; self . lblTitle = lblTitle; self. backg roundColor return self; = [UIColor
 
计算机生成了可选文字:- (void)viewDidLoad { [super viewDidLoad] ; [self. collectionView registerClass: [HMPhotoCollectionViewCell class] forCellWithReuseIdentifier: CGFloat itemW = self. collectionView. bounds. size. width; CGFloat itemH = itemW; = CGSizeMake( itemW, itemH) ; self. flowLayout . itemSize // view [self. collectionView registerClass: [HMSectionHeaderView class] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"header"] ; // Efooter View [self. collectionView registerClass: [HMSectionFooterView class] forSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"] ; // i±MGÆfiheader footerfiX/J\ self. flowLayout . heade rRefe renceSize self. flowLayout. footerReferenceSize = CGSizeMake(0, = CGSizeMake(0, 49) ; 49) ;
计算机生成了可选文字:// footerfi-k/J\, - (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElement0fKind: (NSString *)kind atlndexPath: (NSIndexPath *)indexPath { *model = self. photos [indexPath.section] ; HMPhoto static NSString *ID; 1. header footer if ( [kind isEqualToString:UICollectionElementKindSectionHeader]) { // ID = @lheader"; else { ID = @lfooter"; IjtfÄ 2. — I header 0 footer [collectionView de ueueReusableSu UICollectionReusableView *headerFooterView = withReuseIdentifier:ID forlndexPath: indexPath] ; if ( [kind isEqualToString:UICollectionElementKindSectionHeader]) { // lementar View0fKind: ind HMSectionHeaderView *heade rVw heade rVw. photo = model; } else { HMSectionFooterV iew *foote rVw foote rVw. photo = model; return headerFooterView; headerFooterView; HMSect ionHeade rV iew headerFooterView; HMSectionFooterView
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值