#import
"ViewController.h"
#import "Masonry.h"
@interface ViewController ()< UITableViewDelegate, UITableViewDataSource>
{
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] forHeaderFooterViewReuseIdentifier:@"cell2"];
//cell 的高度
_tableView. rowHeight = 30;
#import "Masonry.h"
@interface ViewController ()< UITableViewDelegate, UITableViewDataSource>
{
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] forHeaderFooterViewReuseIdentifier:@"cell2"];
//cell 的高度
_tableView. rowHeight = 30;
//创建tableFootView这样就只显示我们创建的cell了
_tableView
.
tableFooterView
= [
UITableView
new
];
[ _tableView mas_makeConstraints :^( MASConstraintMaker *make) {
[ _tableView mas_makeConstraints :^( MASConstraintMaker *make) {
//edges
指的是边缘
,就是self.view的边缘
make.
edges
.
equalTo
(
self
.
view
);
}];
_dataList = [ NSMutableArray array ];
}];
_dataList = [ NSMutableArray array ];
//深复制
_dataList
=
@[
@"hello"
,
@"world"
,
@"welcome"
,
@"to"
,
@"changsha"
]
.
mutableCopy
;
_dataList = @[@22 , @12 , @34 , @54 , @66] . mutableCopy ;
}
// 返回多少个 section
-( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView{
return 1 ;
}
// 调用次数没有任何实际意义,用于返回 cell 个数
-( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
return 5 ;
}
-( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
static NSString *cellId = @"cell" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :cellId];
if (!cell) {
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault
reuseIdentifier :cellId];
// 让 cell 的一部分显示背景颜色
UILabel *label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 200 , 5 , 100 , 25 )];
label. tag = 1001 ;
label. backgroundColor = [ UIColor redColor ];
[cell. contentView addSubview :label];
}
UILabel *label = ( UILabel *)[cell. contentView viewWithTag : 1001 ];
_dataList = @[@22 , @12 , @34 , @54 , @66] . mutableCopy ;
}
// 返回多少个 section
-( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView{
return 1 ;
}
// 调用次数没有任何实际意义,用于返回 cell 个数
-( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
return 5 ;
}
-( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
static NSString *cellId = @"cell" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :cellId];
if (!cell) {
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault
reuseIdentifier :cellId];
// 让 cell 的一部分显示背景颜色
UILabel *label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 200 , 5 , 100 , 25 )];
label. tag = 1001 ;
label. backgroundColor = [ UIColor redColor ];
[cell. contentView addSubview :label];
}
UILabel *label = ( UILabel *)[cell. contentView viewWithTag : 1001 ];
/*
显示地1
个
section
和,显现每个
section
中的
cell
的
index*/
label.
text
= [
NSString
stringWithFormat
:
@"%ld--%ld"
,indexPath.
section
,indexPath.
row
];
UIView *tempView = [cell. contentView viewWithTag : 1002 ];
if (indexPath. row == 0 ) {
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 5 , 20 , 30 )];
view. tag = 1002 ;
view. backgroundColor = [ UIColor greenColor ];
[cell. contentView addSubview :view];
}
else {
// 只有当 indexPath.row==0 的时候才显示出 greenColor, 只显示一个 greenColor ,因为上面只是定义了一个 section
[tempView removeFromSuperview ];
}
// 让 cell 有背景颜色
if (indexPath. row == 0 ) {
cell. backgroundColor = [ UIColor blackColor ];
}
else {
cell. backgroundColor = [ UIColor clearColor ];
}
return cell;
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
UIView *tempView = [cell. contentView viewWithTag : 1002 ];
if (indexPath. row == 0 ) {
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 5 , 20 , 30 )];
view. tag = 1002 ;
view. backgroundColor = [ UIColor greenColor ];
[cell. contentView addSubview :view];
}
else {
// 只有当 indexPath.row==0 的时候才显示出 greenColor, 只显示一个 greenColor ,因为上面只是定义了一个 section
[tempView removeFromSuperview ];
}
// 让 cell 有背景颜色
if (indexPath. row == 0 ) {
cell. backgroundColor = [ UIColor blackColor ];
}
else {
cell. backgroundColor = [ UIColor clearColor ];
}
return cell;
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
@end