• 动态切换tableView中的cell的种类


    动态切换tableView中的cell的种类

    为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:)

    效果:

    源码:

    首先,你要准备3种cell,直接继承系统的就行了.

    //
    //  RootViewController.m
    //  ChangeCell
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "YellowCell.h"
    #import "RedCell.h"
    #import "TitleCell.h"
    
    // ------------------------------
    static NSString *CELL[] = {
        @"TitleCellFlag",
        @"RedCellFlag",
        @"YellowCellFlag",
    };
    typedef enum : NSUInteger {
        Title,
        Red,
        Yellow,
    } CellType;
    // ------------------------------
    
    @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
    
    @property (nonatomic, strong) UITableView  *tableView;
    @property (nonatomic, strong) NSString     *changeFlag;  // 切换标签
    
    @property (nonatomic, strong) NSArray      *dataArray;   // 数据源
    @property (nonatomic, strong) NSArray      *redData;     // 红色cell数据
    @property (nonatomic, strong) NSArray      *yellowData;  // 黄色cell数据
    
    @end
    
    @implementation RootViewController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        // 初始化TableView
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        
        // 红色cell数据
        _redData    = @[@"", @"", @"", @""];
        
        // 黄色cell数据
        _yellowData = @[@"", @"", @"", @"", @"", @"", @""];
        
        
        
        // 数据源
        _dataArray = _redData;
        
        // 类型
        _changeFlag = CELL[Red];
        
        // 4秒钟之后切换cell
        [self performSelector:@selector(runSelector:)
                   withObject:nil
                   afterDelay:9];
    }
    
    - (void)runSelector:(id)sender
    {
        // 数据源
        _dataArray = _yellowData;
        
        // 类型
        _changeFlag = CELL[Yellow];
        
        // 重新加载数据
        [_tableView reloadData];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_dataArray count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = nil;
        if (indexPath.row == 0) // 第一格cell
        {
            cell = [[TitleCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:CELL[Title]];
            cell.textLabel.font      = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18];
            cell.textLabel.text      = @"YouXianMing";
            cell.textLabel.textColor = [UIColor redColor];
            cell.selectionStyle      = UITableViewCellSelectionStyleNone;
        }
        
        if (indexPath.row != 0) // 其他cell
        {
            if ([_changeFlag isEqualToString:CELL[Red]])
            {
                cell = [[RedCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CELL[Title]];
                cell.backgroundColor = [UIColor redColor];   // 红色
                cell.selectionStyle  = UITableViewCellSelectionStyleNone;
            }
            
            if ([_changeFlag isEqualToString:CELL[Yellow]])
            {
                cell = [[YellowCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:CELL[Title]];
                cell.backgroundColor = [UIColor yellowColor]; // 黄色
                cell.selectionStyle  = UITableViewCellSelectionStyleNone;
            }
        }
        
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row == 0)
        {
            return 70;
        }
        
        if ([_changeFlag isEqualToString:CELL[Red]])
        {
            return 100;
        }
        
        if ([_changeFlag isEqualToString:CELL[Yellow]])
        {
            return 200;
        }
        
        return 0;
    }
    
    @end

    分析:

    用这个来标示重用吧

    有一个标签是用来切换cell类型的,以及对应的数据源

    根据切换标签来决定初始化哪一种cell

    就是这样子实现的.

     
     
     
     
  • 相关阅读:
    Ubuntu下systemd服务的配置
    编译压缩代码 MFCompress-src-1.01 :对‘***’未定义的引用
    德尔福 基础
    德尔福 XE5 安卓权限设置
    德尔福 XE5 安卓调试
    复制任意文件或文件夹到剪贴板
    无法完成安装:'Cannot access storage file '/
    Centos7.4安装kvm虚拟机(使用virt-manager管理)
    MSYS2 使用
    线程
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3939466.html
Copyright © 2020-2023  润新知