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

原文地址:https://www.cnblogs.com/YouXianMing/p/3911706.html