• 将一种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 :)

  • 相关阅读:
    SQL查询效率100w数据查询只要1秒
    超级实用且不花哨的js代码大全 (四) JavaScript[对象.属性]集锦
    Sql Server实用操作维护小技巧集合
    asp.net截取字符串方法
    自己整理的asp.net 缓存 相关资料
    【译】初识SSRS 通向报表服务的阶梯系列(一)
    【译】无处不在的数据 通向报表服务的阶梯系列(三)
    【译】SSRS基础 通向报表服务的阶梯系列(二)
    浅谈SQL Server中的事务日志(三)在简单恢复模式下日志的角色
    SQL Server中生成测试数据
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3911706.html
Copyright © 2020-2023  润新知