UItable配合数组的简单用法

#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;

    //创建tableFootView这样就只显示我们创建的cell了
    _tableView . tableFooterView = [ UITableView new ];
   
    [
_tableView mas_makeConstraints :^( MASConstraintMaker *make) {
      
        //edges 指的是边缘 ,就是self.view的边缘
        make. edges . equalTo ( self . view );

    }];
   
_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 ];
   
    /* 显示地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.
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值