zoukankan      html  css  js  c++  java
  • 将一种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 :)

  • 相关阅读:
    IOS sqlite数据库增删改查
    宏定义偷懒型set,get
    创业白手起家也须要条件——北漂18年(14)
    Python模拟登录wap版百度贴吧+自己主动回贴
    纯CSS制作冒泡提示框
    tiny210(s5pv210)移植u-boot(基于 2014.4 版本号)——移植u-boot.bin(打印串口控制台)
    ZOJ 3587 扩展KMP
    用选择法对10个整数按从小到大排序(数组)
    设计一个算法,推断一个二叉树是否为全然二叉树
    Hibernate学习笔记(八) — 懒载入与抓取策略
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3911706.html
Copyright © 2011-2022 走看看