• 封装TableView有可能用到的数据结构和UITableViewCell的一个继承类


    最近4年的时间,我已经做了5个App完全独立开发, 工作经历5个App, 维护了两个App.

    在这期间用的最多的是UITableView, 因此也有许多感觉可以封装的.

    现在就是我封装的.

    RXCell 继承于UITableViewCell

    // 所以TableViewCell的基类
    @interface RXCell : UITableViewCell
    // 用data更新cell数据
    - (void)setData:(id)data;
    // 从xib中获取cell,记得在xib设置identify
    + (id)cell_xib;
    // 用initWithStyle:reuseIdentifier:方式获取cell
    + (instancetype)cell;
    // cell 的高度,需要子类去实现
    + (CGFloat)cellHeight;
    // 默认的是Class的字符串,子类不需要重写
    + (NSString *)identifier;
    @end
    
    
    @implementation RXCell
    - (void)setData:(id)data
    {
        // Do Nothing
    }+ (id)cell_xib
    {
        NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:[self identifier] owner:nil options:nil];
        return nibView[0];
    }
    + (instancetype)cell
    {
        Class cls = [self class];
        id cell = [[cls alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self identifier]];
        return cell;
    }
    + (CGFloat)cellHeight
    {
        return 0;
    }
    + (NSString *)identifier
    {
        return NSStringFromClass([self class]);
    }
    @end

    RXFunctionItem

    @interface RXFunctionItem : NSObject
    @property (nonatomic, copy) NSString *iconName;     // 图片名称
    @property (nonatomic, copy) NSString *title;        // 名称
    @property (nonatomic, assign) SEL action;           // action
    @property (nonatomic, assign) int type;             // type
    @property (nonatomic, strong) id object;            // 扩展数据
    - (id)initWithIconName:(NSString *)iconName title:(NSString *)title action:(SEL)action type:(int)type;
    - (id)initWithIconName:(NSString *)iconName title:(NSString *)title action:(SEL)action type:(int)type object:(id)object;
    @end

    在自定义的TestCell需要添加代码:

    @interface TestCell ()
    @property (weak, nonatomic) IBOutlet UILabel *lblTitle;
    @end
    @implementation TestCell
    - (void)setData:(id)data
    {
        if ([data isKindOfClass:[RXFunctionItem class]]) {
            RXFunctionItem *tmp = data;
            self.lblTitle.text = tmp.title;
        }
    }
    - (void)awakeFromNib {
        // Initialization code
        self.lblTitle.backgroundColor = [UIColor redColor];
    }
    + (CGFloat)cellHeight
    {
        return 50;
    }
    @end

    在VC中使用:

    #pragma mark - UITableViewDataSource
    #pragma mark Required
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.functionItems.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        id item = self.functionItems[indexPath.row];
        NSString *identify = [TestCell identifier];
        TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:identify];
        if (cell == nil) {
            cell = [TestCell cell_xib];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setData:item];
        return cell;
    }
    #pragma mark - UITableViewDelegate
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return [TestCell cellHeight];
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        RXFunctionItem *item = self.functionItems[indexPath.row];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        if (item.action != nil) {
            [self performSelector:item.action withObject:item afterDelay:0];
        }
    }
    #pragma mark - Action
    - (void)cell0Action:(id)sender
    {
        NSLog(@"cell0Action:%@", sender);
    }
    - (void)cell1Action:(id)sender
    {
        NSLog(@"cell1Action:%@", sender);
    }
    - (void)cell2Action:(id)sender
    {
        NSLog(@"cell2Action:%@", sender);
    }
    #pragma mark - View Life Cycle
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        RXFunctionItem *item0 = [[RXFunctionItem alloc] initWithIconName:@"" title:@"第1行" action:@selector(cell0Action:) type:0 object:nil];
        RXFunctionItem *item1 = [[RXFunctionItem alloc] initWithIconName:@"" title:@"第2行" action:@selector(cell1Action:) type:0 object:nil];
        RXFunctionItem *item2 = [[RXFunctionItem alloc] initWithIconName:@"" title:@"第3行" action:@selector(cell2Action:) type:0 object:nil];
        self.functionItems = @[item0, item1, item2];
        [self.tableView reloadData];
    }

    思路是:

    VC 用RXFunctionItem准备好数据, 然后给UITableViewDataSource和UITableViewDelegate使用,

    在cellForRowAtIndexPath中把数据取出来, 然后Cell取刷新数据.

    参考: https://github.com/xzjxylophone/RXTableViewItem

  • 相关阅读:
    HTML常用元素标签
    属性列表-边框属性
    (5)opencv的基础操作和矩阵的掩模操作
    (4)关于Alpha通道问题
    (3)关于命名空间的问题
    (2)关于opencv解压
    (1)opencv的安装和遇到的问题
    给adobe acrobat reader 添加图片注释
    安装adobe reader阅读器
    apache安装和mysql php配置问题
  • 原文地址:https://www.cnblogs.com/xzjxylophone/p/4981097.html
Copyright © 2020-2023  润新知