將一種cell當做幾種cell使用

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   io   strong   for   

將一種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 :)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.