将一种cell当做几种cell使用

简介:

将一种cell当做几种cell使用

将一种cell当做几种cell用是有着一些意义的,比如,有时候,不同的cell之间差异很小,如果再派生一个cell出来,就会显得很麻烦,这时候,将这个cell当做几个cell用才有市场的说:)

效果:

源码:

ModelCell.h 与 ModelCell.m

//
//  ModelCell.h
//  Cells
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ModelCell : UITableViewCell

@property (nonatomic, assign) BOOL  show1;
@property (nonatomic, assign) BOOL  show2;
@property (nonatomic, assign) BOOL  show3;

@end

RootViewController.m
//
//  RootViewController.m
//  Cells
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "ModelCell.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) NSArray      *dataSource;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // tableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                              style:UITableViewStylePlain];
    _tableView.dataSource     = self;
    _tableView.delegate       = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

#pragma mark - UITableView's dataSource & delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedFlag = @"YouXianMing";
    ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag];
    if (cell == nil) {
        cell = [[ModelCell alloc] initWithStyle:UITableViewCellStyleDefault
                                reuseIdentifier:reusedFlag];
    }
    
    cell.show1 = YES;
    cell.show2 = YES;
    cell.show3 = YES;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.f;
}

@end

核心的地方:

根据属性的setter方法动态隐藏cell中的某些控件

然后在配置cell的时候动态设定要显示的控件

just so easy :)

目录
相关文章
|
1月前
|
移动开发 JavaScript 小程序
uView Cell 单元格
uView Cell 单元格
52 1
|
2月前
element-plus table表格cell-style的使用
element-plus table表格cell-style的使用
34 1
TableView列表展开不同cell
TableView列表展开不同cell
87 0
TableView列表展开不同cell
Cell里面10个cell只想展示6条
Cell里面10个cell只想展示6条
96 0
Cell里面10个cell只想展示6条
POI 获取 Cell 值
public static String getCellValue(Cell cell) { String cellValue = ""; // 以下是判断数据的类型 switch (cell.
1502 0