iOS UITableView(二) 为tableView添加数据

来源:互联网 发布:fiddler拦截修改数据 编辑:程序博客网 时间:2024/05/22 09:40

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



#import "ViewController.h"

//代理

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

   UITableView *_tableView;

    //创建数据源数组

   NSMutableArray *_dataArry;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfcreatData];

    [selfcreatTableView];

}

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

-(void)creatData{

    _dataArry=[[NSMutableArrayalloc]init];

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

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

        NSMutableArray *arr=[[NSMutableArrayalloc]init];

        

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

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

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

            [arraddObject:str];

        }

        [_dataArryaddObject:arr];

    }

}

-(void)creatTableView{

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

   // self.automaticallyAdjustsScrollViewInsets = NO;

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

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

    //设置数据源

    _tableView.dataSource =self;

    //设置代理

    _tableView.delegate =self;

    [self.viewaddSubview:_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 =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifire];

    }

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

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

   return cell;

}

//设置头标

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

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

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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 = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID] autorelease];

    }

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

   /*

    //获取一维数组

    NSArray *arr = _dataArr[indexPath.section];

    f

    */

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

    cell.imageView.image = [UIImageimageNamed:model.headName];

    cell.textLabel.text = model.userName;

    cell.detailTextLabel.text = model.phoneNumber;

    

   return cell;

}




cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:identifire];



cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:identifire];



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


0 0