iOS中大流中的自定义cell 技术分享

AppDelegate.m指定根视图

 self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];
//根视图

RootTableViewController.m

#import "RootTableViewController.h"
#import "TestCell.h"
#import "TestModel.h"

@interface RootTableViewController ()

@property (nonatomic, strong) NSMutableArray *datasourceArray;

@end

@implementation RootTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.datasourceArray = [NSMutableArray array];
    
    [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"cell"];
    
    for (int i = 0; i < 50; i++) {
        TestModel *model = [TestModel new];
        model.isShow = NO;
        [self.datasourceArray addObject:model];
    }

    
}

#pragma mark - Table view data source

数据源方法
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.datasourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    TestModel *model = self.datasourceArray[indexPath.row];
    
    if (model.isShow) {
        
        cell.label.text = @"展示view";
        [cell addView];
    } else {
        cell.label.text = @"什么都没有";
        [cell removeView];
    }

    
    return cell;
}

返回高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestModel *model = self.datasourceArray[indexPath.row];
    if (model.isShow) {
        return 300;
    } else {
        return 100;
    }
}
点击cell触发的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestModel *model = self.datasourceArray[indexPath.row];
    model.isShow = !model.isShow;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
准备一个自定义cell

#import <UIKit/UIKit.h>

@interface TestCell : UITableViewCell

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *redView;

- (void)addView;
- (void)removeView;

@end

#import "TestCell.h"

@implementation TestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllViews];
    }
    return self;
}

- (void)addAllViews
{
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    self.label.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.label];
    
    self.redView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200)];
    self.redView.backgroundColor = [UIColor redColor];
    
}

- (void)addView
{
    [self addSubview:self.redView];
}

- (void)removeView
{
    [self.redView removeFromSuperview];
}

准备一个model类

#import <Foundation/Foundation.h>

@interface TestModel : NSObject

@property (nonatomic, assign) BOOL isShow;

@end

最终效果如下:




有好的建议和问题可微博私信:http://weibo.com/hanjunqiang


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩俊强

奖励一杯咖啡☕️

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值