tableview的常用mvc框架模板

SchoolViewController.h

1
2
3
4
5
@interface : UIViewController
@end

SchoolViewController.m

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
82
83
84
85
86
87
88
89
90
91
92
93
94
#import "SchoolViewController.h"
#import "SchoolTableViewCell.h"
#import "SchoolModel.h"
#import "CollocationDetailController.h"
@interface ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableview;
@property (nonatomic,strong)NSMutableArray *dataArry;
@end
@implementation
-(NSMutableArray *)dataArry{
if (!_dataArry) {
_dataArry = [NSMutableArray array];
}
return _dataArry;
}
- (void)viewDidLoad {
[super viewDidLoad];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.navigationItem.title = @"专题列表";
self.view.backgroundColor = [UIColor whiteColor];
[self network];
[self setupTableview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setupTableview{
self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH -64) style:UITableViewStyleGrouped];
self.tableview.delegate = self;
self.tableview.dataSource = self;
self.tableview.showsVerticalScrollIndicator = NO;
self.tableview.scrollEnabled = YES;
self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableview.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.tableview];
}
#pragma mark -- tableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SchoolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell){
cell = [[SchoolTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
SchoolModel *model = self.dataArry[indexPath.row];
cell.model = model;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SchoolModel *model = self.dataArry[indexPath.row];
CollocationDetailController *detail = [[CollocationDetailController alloc]init];
detail.guid = model.special_guid;
detail.thum = model.special_thumb;
detail.indexpath = indexPath.row;
detail.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:detail animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.001;
}
-(void)network{
//获取网络数据,并赋值
}
@end

SchoolModel.h

1
2
3
4
5
6
7
8
9
10
11
#import <Foundation/Foundation.h>
@interface SchoolModel : NSObject
@property (nonatomic,strong)NSString *special_content;
@property (nonatomic,strong)NSString *special_guid;
@property (nonatomic,strong)NSString *special_thumb;
@property (nonatomic,strong)NSString *special_title;
@property (nonatomic,strong)NSString *special_type;
@end

SchoolModel.m

1
2
3
4
5
#import "SchoolModel.h"
@implementation SchoolModel
@end

SchoolTableViewCell.h

1
2
3
4
5
6
7
8
9
10
11
@class SchoolModel;
@interface SchoolTableViewCell : UITableViewCell
@property(nonatomic,strong)UIImageView *imgView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *detailLabel;
@property(nonatomic,strong)UIView *line;
@property(nonatomic,strong)SchoolModel *model;
@end

SchoolTableViewCell.m

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
#import "SchoolTableViewCell.h"
#import "SchoolModel.h"
@implementation SchoolTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"aaa"]];
[self.contentView addSubview:self.imgView];
self.titleLabel = [[UILabel alloc]init];
self.titleLabel.textColor = UIColorFromRGB(0x333333);
self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
[self.contentView addSubview:self.titleLabel];
self.detailLabel = [[UILabel alloc]init];
self.detailLabel.textColor = UIColorFromRGB(0x999999);
self.detailLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
[self.contentView addSubview:self.detailLabel];
self.line = [[UIView alloc]init];
self.line.backgroundColor = UIColorFromRGB(0xe5e5e5);
[self.contentView addSubview:self.line];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(CGSizeMake(100,100));
make.left.equalTo(self.contentView.left).offset(10);
make.top.equalTo(self.contentView.top).offset(10);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(CGSizeMake(ScreenW - 130,33));
make.left.equalTo(self.imgView.right).offset(10);
make.top.equalTo(self.contentView.top).offset(10);
}];
[self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(CGSizeMake(ScreenW - 130,31));
make.left.equalTo(self.imgView.right).offset(10);
make.top.equalTo(self.titleLabel.bottom).offset(10);
}];
[self.line mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(CGSizeMake(ScreenW,1));
make.left.equalTo(self.contentView.left).offset(0);
make.top.equalTo(self.contentView.bottom).offset(-1);
}];
}
-(void)setModel:(SchoolModel *)model{
[self.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.special_thumb]]];
self.titleLabel.text = model.special_title;
self.detailLabel.text = model.special_content;
}
@end