ios中列表UITableView的使用

来源:互联网 发布:酒店市场数据分析 编辑:程序博客网 时间:2024/05/20 02:54

UITableView的使用流程

首先在代码中实现两个代理UITableViewDelegate,UITableViewDataSource。

@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>@end

然后创建UITableView类,并设置代理。

    UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0,0, 375, 667-64) style:UITableViewStylePlain];    [self.view addSubview:table];    table.delegate=self;    table.dataSource=self;    //添加表头    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 100)];    imageview.image=[UIImage imageNamed:@"163631.jpg"];    table.tableHeaderView=imageview;

实现代理的方法。

//UITableView的代理方法-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //创建静态标识符    static NSString *identife=@"cell";    //根据标识符从重用池中取cell    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identife];    //如果没有取到就创建一个新的    if(cell ==nil){        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identife];    }    //对cell进行赋值    cell.textLabel.text=@"姜帅杰";    cell.detailTextLabel.text=@"姜帅杰哈哈";    cell.imageView.image=[UIImage imageNamed:@"163631.jpg"];    cell.accessoryType=UITableViewCellAccessoryDetailButton;    //当在第三行时    if(indexPath.row==3){    }    return cell;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 10;}//cell的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 160;}

使用数组为UITableView填充内容

首先你需要创建一个数组,并为其填入内容。

    NSArray *data;    data=[NSArray arrayWithObjects:@"盖伦",@"剑圣",nil];

然后在每个cell中设置值。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //创建静态标识符    static NSString *identife=@"cell";    //根据标识符从重用池中取cell    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identife];    //如果没有取到就创建一个新的    if(cell ==nil){        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identife];    }    //对cell进行赋值    cell.textLabel.text=[data objectAtIndex:indexPath.row];    cell.detailTextLabel.text=@"牛逼";    cell.imageView.image=[UIImage imageNamed:@"163631.jpg"];    cell.accessoryType=UITableViewCellAccessoryDetailButton;    return cell;}

最常用的就是使用对象数组,可以为每个cell赋值更多的内容。

控制分区的代理方法

//分区的个数-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 3;}//分区的高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 40;}//分区的标题-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return @"分区";}

UICollectionView也是差不多的。

原创粉丝点击