zoukankan      html  css  js  c++  java
  • 动态切换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

    就是这样子实现的.

     
     
     
     
  • 相关阅读:
    xshell的安装及连接linux的使用方法
    linux中yum install 命令无效
    linux-centOS环境下安装jdk8
    centOS不显示ipv4地址的解决办法
    centOS开启和关闭防火墙
    java-分布式-索引
    java-网络通信-索引
    java-中间件
    java-框架-索引
    JVM-索引
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3939466.html
Copyright © 2011-2022 走看看