iOS UITableView(二) 为tableView添加数据

//我们上一章创建了一个tableView我们来添加点数据吧



#import "ViewController.h"

//代理

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    UITableView *_tableView;

    //创建数据源数组

    NSMutableArray *_dataArry;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self creatData];

    [self creatTableView];

}

//我们这里来设计一个二维数组

-(void)creatData{

    _dataArry=[[NSMutableArray alloc]init];

    for (int i = 0;i < 10; i ++ ) {

        //创建一维数组存储每个分区的cell数据

        NSMutableArray *arr=[[NSMutableArray alloc]init];

        

        for (int j = 0; j < 10; j++) {

            //存储每一行cell的数据,每一行的cell的数据模型

            NSString *str =[NSString stringWithFormat:@"这是第%d组第%d",i,j];

            [arr addObject:str];

        }

        [_dataArry addObject:arr];

    }

}

-(void)creatTableView{

    //下面注释这句我们可以慢慢体会以后会给各位解答

   // self.automaticallyAdjustsScrollViewInsets = NO;

//此处的UITableViewStylePlain,UITableViewStyleGrouped大家可以自己试验体会下

    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    //设置数据源

    _tableView.dataSource =self;

    //设置代理

    _tableView.delegate = self;

    [self.view addSubview:_tableView];

}

#pragma mark - UITableViewDataSource协议

//设置有多少分区

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _dataArry.count;

}

//每个分区有多少行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_dataArry[section] count];

}

//获取cell  每次显示cell 之前都要调用这个方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //创建复用标识符

    static NSString *identifire = @"identifier";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifire];

    if (!cell) {//如果没有可以复用的

        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifire];

    }

    //填充cell  把数据模型中的存储数据 填充到cell

    cell.textLabel.text=_dataArry[indexPath.section][indexPath.row];

    return cell;

}

//设置头标

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"这是第%ld",section];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


//下面来看看效果吧


下面我们来认识一下cell的风格 上面的是第一种

//    UITableViewCellStyleDefault,// Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)

//    UITableViewCellStyleValue1,// Left aligned label on left and right aligned label on right with blue text (Used in Settings)

//    UITableViewCellStyleValue2,// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)

//    UITableViewCellStyleSubtitle// Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //创建 复用标识符

    static NSString *cellID = @"cellID";

    //

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {//如果没有可复用的

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];

    }

    //填充cell  把数据模型中的存储数据 填充到cell

    /*

    //获取一维数组

    NSArray *arr = _dataArr[indexPath.section];

    f

    */

    UserModel *model = _dataArr[indexPath.section][indexPath.row];

    cell.imageView.image = [UIImage imageNamed:model.headName];

    cell.textLabel.text = model.userName;

    cell.detailTextLabel.text = model.phoneNumber;

    

    return cell;

}




cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifire];



cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifire];



大家可以自己体会 明天决定多写 写一个类似电话薄的东西添加图片和编辑功能


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值