swift代码实现TableView

前言

继续接着上一篇OC代码实现TableView来写我们的swift代码,通过swift代码实现一个简单的TableView 。
swift代码较OC代码,从代码量上会少很多,毕竟没有了.h文件,具体OC与swfit有哪些区别大家可以自行百度,文章也有很多。

Demo比较简单,就是一个普通的TableView,自定义的cell样式,数据都是写死的,没有数据请求。

上代码

//
//  ViewController.m
//  WMOCTableView
//
//  Created by Mengmeng Wang on 2022/6/20.
//

#import "ViewController.h"
#import "WMCell.h"

@interface ViewController () 

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView  alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor lightGrayColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

- (WMCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ide = @"cell";
    WMCell *cell = [tableView dequeueReusableCellWithIdentifier:ide];
    if (cell == nil) {
        cell = [[WMCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ide];
    }
    return cell;
}


@end

//
//  WMCell.m
//  WMOCTableView
//
//  Created by Mengmeng Wang on 2022/6/20.
//

#import "WMCell.h"

@interface WMCell()

@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *msgLabel;

@end


@implementation WMCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.backView];
        [self.backView addSubview:self.iconImageView];
        [self.backView addSubview:self.titleLabel];
        [self.backView addSubview:self.msgLabel];
    }
    return self;
}

#pragma mark - get
- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, 130);
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 15;
    }
    return _backView;
}

- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.frame = CGRectMake(10, 10, 60, 60);
        _iconImageView.backgroundColor = [UIColor redColor];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame) + 10, 10, 250, 20);
        _titleLabel.text = @"阿斯顿发发电房打发的发送到发送到发送到发斯蒂芬啊阿斯顿发大法师打发";
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:17];
    }
    return _titleLabel;
}

- (UILabel *)msgLabel{
    if (!_msgLabel) {
        _msgLabel = [[UILabel alloc] init];
        _msgLabel.frame =  CGRectMake(CGRectGetMaxX(self.iconImageView.frame) + 10, CGRectGetMaxY(self.titleLabel.frame) + 10, 250, 40);
        _msgLabel.text = @"adfadfafafadfadfasdfasdfasdflasdfkasdfjalskdfjaslkdfjasdklfajsdflkasjdflaksdfjaskldfjalskdfjadslkfjasdklfajsdlfkasdjfs";
        _msgLabel.textColor = [UIColor blackColor];
        _msgLabel.numberOfLines = 2;
        _msgLabel.font = [UIFont systemFontOfSize:14];
    }
    return _msgLabel;
}

@end

你可能感兴趣的:(swift代码实现TableView)