UItable的创建和简单的理解

#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 ];
    
/*注册*/
    [ _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 ;
}

- (void)didReceiveMemoryWarning {
    [ super didReceiveMemoryWarning ];
   
// Dispose of any resources that can be recreated.
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值