JLChen
2021-02-03 4715e99a9be1c50d8ec31f594af9ebde18647c94
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
//  EZCustomTableVIew.m
//  EZOpenSDKDemo
//
//  Created by yuqian on 2019/6/27.
//  Copyright © 2019 Ezviz. All rights reserved.
//
 
#import "EZCustomTableView.h"
#import "Masonry.h"
 
 
@interface EZCustomTableView() <UITableViewDelegate,UITableViewDataSource>
 
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *datasources;
 
@end
 
static NSString *reuseId = @"UITableViewCell";
 
@implementation EZCustomTableView
 
- (instancetype) initTableViewWith:(NSArray *)datasource delegate:(id<EZCustomTableViewDelegate>) delegate {
    
    if (self = [super init]) {
        
        self.tableView = [[UITableView alloc]init];
        self.tableView.backgroundColor = [UIColor colorWithRed:25/255.0 green:25/255.0 blue:112/255.0 alpha:0.5];
        self.tableView.rowHeight = 30;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseId];
        [self addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        self.delegate = delegate;
        self.datasources = datasource;
        
        [self.tableView reloadData];
    }
    return self;
}
 
- (void) destroy {
    
    [self removeFromSuperview];
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasources.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId forIndexPath:indexPath];
    
    cell.textLabel.text = self.datasources[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor colorWithRed:25/255.0 green:25/255.0 blue:112/255.0 alpha:0.5];
    
    return cell;
}
 
 
#pragma mark - UITableViewDelegate
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.delegate respondsToSelector:@selector(EZCustomTableView:didSelectedTableViewCell:)]) {
        [self.delegate EZCustomTableView:self didSelectedTableViewCell:indexPath];
    }
}
 
 
@end