動態切換tableView中的cell的種類

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   ar   for   

動態切換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

就是這樣子實現的.

 

 

    

動態切換tableView中的cell的種類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.