IOS之导航控制器传值

  UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。大家先看一下两者的应用

1、首先我们介绍一下平铺的tableView,初始化一个tableView如下

#pragma mark - 设置子视图
- (void)setSubviews{

    UITableView * table=[[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStylePlain];
    self.tableView=table;
 
    //设置数据源代理
    table.dataSource=self;
    //设置方法属性代理
    table.delegate=self;
  
    [self.view addSubview:table];

}

tableView需要设置两个代理,而要作为tableView的代理必须实现其代理方法,并遵守协议

/**
   tableView 代理功能:
      1 需要告知展示数据的条数
      2 需要告知展示的内容
 
    要想作为tableView的代理 需要遵守UITableViewDataSource 和 UITableViewDelegate两个协议
 */


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

/** 数据数组*/
@property(nonatomic,strong) NSArray * dataArray;

/** tableView接口*/
@property(nonatomic,weak) UITableView * tableView;

@end

2、我们通过当前的系统字体作为显示的内容,加载数据如下

#pragma mark - 加载数据
- (void)loadData{
  self.dataArray=[UIFont familyNames];
 
}

3、实现代理方法

#pragma mark - UITableViewDatasSource

//返回记录条数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}



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

  
    // 重复利用标识
    NSString * identy=@"JRCell";
    
    //从缓冲池获取可以利用的cell
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];
    
    //如果缓冲池没有可利用对象需要重新创建
    if (cell==nil) {
       cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
        
         cell.selectionStyle=UITableViewCellSelectionStyleNone;
 
    }
 
    cell.textLabel.text=self.dataArray[indexPath.row];

    cell.textLabel.font=[UIFont fontWithName:self.dataArray[indexPath.row] size:18];
 
    return cell;
}

4、效果图如下

  5、最简单的tableView 我们就做完了,但是在日常开发中,我们需要用到的功能不仅仅这么简单,有的时候cell是需要自定义的,下面我们完成一个美团列表展示自己定义的tableView

6、这里要求我们自定义美团列表cell,对于数据的加载和读取这里不做介绍,我们把重点放在如何自定义cell上面

① 我们先自定义一个cell,并且继承了UITableViewCell

② 然后我们向当前cell中拼接子视图

#pragma mark - 增加子视图
- (void) setSubview{
    
   // 1 增加图标
    UIImageView * jrImageView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 5, kRowHeight-10, kRowHeight-10)];
    self.jrImageView=jrImageView;
    jrImageView.backgroundColor=[UIColor redColor];
    [self.contentView addSubview:jrImageView];
    
    // 2 增加标题
    UILabel * titleLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jrImageView.frame)+10, 10, kWidth-CGRectGetMaxX(jrImageView.frame)-10, 20)];
    titleLable.text=@"汉金城烤肉自助餐厅";
    titleLable.font=[UIFont boldSystemFontOfSize:18];
    self.jrTitleLable=titleLable;
    [self.contentView addSubview:titleLable];
    
    // 3 增加子标题
    UILabel * detailLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jrImageView.frame)+10, CGRectGetMaxY(titleLable.frame), kWidth-CGRectGetMaxX(jrImageView.frame)-10, 45)];
    detailLable.text=@"汉金城烤肉自助餐厅汉金城烤肉自助餐厅汉金";
    detailLable.numberOfLines=0;
    detailLable.font=[UIFont boldSystemFontOfSize:16];
    detailLable.textColor=[UIColor grayColor];
    self.jrDetailLable=detailLable;
    [self.contentView addSubview:detailLable];
    
    // 4 增加价格
    UILabel * priceLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jrImageView.frame)+10, CGRectGetMaxY(detailLable.frame), 60, 25)];
    priceLable.text=@"$2.9";
    priceLable.font=[UIFont boldSystemFontOfSize:22];
    priceLable.textColor=[UIColor colorWithRed:30/255.0 green:180/255.0 blue:150/255.0 alpha:1];
    self.jrPriceLale=priceLable;
    [self.contentView addSubview:priceLable];
    
    // 5 已售数量
    UILabel * sellLable=[[UILabel alloc] initWithFrame:CGRectMake(kWidth-70, CGRectGetMaxY(detailLable.frame), 60, 25)];
    sellLable.text=@"已售1150";
    sellLable.font=[UIFont boldSystemFontOfSize:13];
    sellLable.textColor=[UIColor grayColor];
    self.jrSellLable=sellLable;

    [self.contentView addSubview:sellLable];
    
 
}

③ 我们需要对子视图开辟接口出来让外界访问

④ 定义方法初始化数据

#pragma mark - 初始化数据
- (void) initDataWithInfo:(Information *) info{
    
    //设置图标
    self.jrImageView.image=[UIImage imageNamed:info.strPic];
    
    //设置标题
    self.jrTitleLable.text=info.title;
    
    //设置明细
    self.jrDetailLable.text=info.detailTitle;
    
    //设置价格
    self.jrPriceLale.text=[NSString stringWithFormat:@"$%.1f",info.price];
    
    //设置销量
    self.jrSellLable.text=[NSString stringWithFormat:@"销量%ld",info.soldNum];
    
    
}

⑤ 我们在代理方法里面,初始化我们自定义cell并且设置数据即可

#pragma mark 返回cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    NSString * identy=@"jrCell";
    
    JRTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];
    
    if (cell==nil) {
        
        cell=[[JRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
        
        //设置选中样式为空
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }

    //重新设置数据
    Information *info= self.dataArray[indexPath.row];
    [cell initDataWithInfo:info];


    return cell;
    

}
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归杰瑞教育技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:JRedu技术交流
 
原文地址:https://www.cnblogs.com/jerehedu/p/4890922.html