#import
"ViewController.h"
#import "Masonry.h"
@interface ViewController ()< UITableViewDataSource , UITableViewDelegate >
{
UITableView *_tableView;
NSMutableArray *_dataList;
}
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
_tableView = [[ UITableView alloc ] initWithFrame : CGRectZero style : UITableViewStylePlain ];
_tableView . delegate = self ;
_tableView . dataSource = self ;
[ self . view addSubview : _tableView ];
#import "Masonry.h"
@interface ViewController ()< UITableViewDataSource , UITableViewDelegate >
{
UITableView *_tableView;
NSMutableArray *_dataList;
}
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
_tableView = [[ UITableView alloc ] initWithFrame : CGRectZero style : UITableViewStylePlain ];
_tableView . delegate = self ;
_tableView . dataSource = self ;
[ self . view addSubview : _tableView ];
/*注册*/
[
_tableView
registerClass
:[
UITableViewCell
class
]
forCellReuseIdentifier
:
@"cell"
];
_tableView . rowHeight = 30 ;
_tableView . tableFooterView = [ UIView new ];
[ _tableView mas_makeConstraints :^( MASConstraintMaker *make) {
make. edges . equalTo ( self . view );
}];
}
#pragma mark - row 的个数
// 调用次数没有任何实际意义,用以返回 cell 个数
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
return 20 ;
}
// 返回 cell, 调用次数无限制,取决于用户
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier : @"cell" ];
cell2. textLabel . text = [ NSString stringWithFormat : @"%ld" ,indexPath. row ];
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 5 , 20 , 30 )];
view. tag = 1001 ;
view. backgroundColor = [ UIColor redColor ];
[cell2. contentView addSubview :view];
return cell2;
}
// 哪一行被点击
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
NSLog ( @"%ld 被点击 " ,indexPath. row );
}
// 每一 section 的高度
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
return 0.01 ;
}
// 每一行的高度
- ( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath{
if (indexPath. row % 2 == 0 ){
return 50 ;
}
return 100 ;
}
_tableView . rowHeight = 30 ;
_tableView . tableFooterView = [ UIView new ];
[ _tableView mas_makeConstraints :^( MASConstraintMaker *make) {
make. edges . equalTo ( self . view );
}];
}
#pragma mark - row 的个数
// 调用次数没有任何实际意义,用以返回 cell 个数
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
return 20 ;
}
// 返回 cell, 调用次数无限制,取决于用户
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier : @"cell" ];
cell2. textLabel . text = [ NSString stringWithFormat : @"%ld" ,indexPath. row ];
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 5 , 20 , 30 )];
view. tag = 1001 ;
view. backgroundColor = [ UIColor redColor ];
[cell2. contentView addSubview :view];
return cell2;
}
// 哪一行被点击
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
NSLog ( @"%ld 被点击 " ,indexPath. row );
}
// 每一 section 的高度
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
return 0.01 ;
}
// 每一行的高度
- ( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath{
if (indexPath. row % 2 == 0 ){
return 50 ;
}
return 100 ;
}
- (void)didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning
];
// Dispose of any resources that can be recreated.
}
// Dispose of any resources that can be recreated.
}
@end