OC语言——————表视图

表视图UITableView

使用沙盒获取图片和plist文件的路径,调用类方法:(NSString *)[[NSBundle mainbundle] pathForResource:(NSString *)ofType:];

接下来就是表视图UITableView;

手写的时候,需要先实例化一个tableView,(UITableView *)[[UITableView alloc] initWithFrame:(CGRect) style:(EnUITableViewStyle)];

UITableViewSytlePlain(简约风格)和UITableViewStyleGrouped(分组风格);

加载到父视图[self.view addSubview:(UITableView *)];

需要遵守两个协议:UITableViewDataResource和UITableViewDelegate

实现UITableViewDataSource下面两个必须实现的方法:

//设置行数

-(NSInteger)tableView:(UITableview *)tableView numberOfRowsInSection:(NSInteger)section;

//绘制单元格(cell)

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

{

//单元格的复用,避免重复创建对象

static NSString *reuse=@“cell”;

//从队列里面实例化一个UITableViewCell的对象

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];

if(cell==nil)

{

//实例化的时候,单元格的显示风格有四种:1.UITableViewCellStyleDefault 默认风格,仅显示一行文字

2.UITableViewCellStyleValue1 存在imageView,textLabel,detailTextLabel,细节描述在最右边

3.UITableViewValue2 不存在imageView

4.UITableViewSubtitle 存在imageView,textLabel,detailTextLabel,细节描述位于textLabel 的下方

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

 

}

cell.textLabel.text  //设置单元格显示的内容

cell.imageView.image   //设置单元格里面显示的图片内容

cell.detailTextlabel.text  //设置细节描述

return cell;

 

}

需要设置代理和数据源【delegate,datasource】的对象,才能触发协议里面的方法;

还可以设置分组的组数:

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

设置行高:

-(CGFloat )tableView :( UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 行高;

}

处理cell里面自带imageView的大小的方法:

   cell.imageView.image = [UIImage imageNamed:@"光影.jpg"];

    CGSize itemSize = CGSizeMake(80, 80);

    UIGraphicsBeginImageContext(itemSize);

    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);

    [cell.imageView.image drawInRect:imageRect];

    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

 

后面学习的UITableViewController,需要直接添加一个UITableViewController,然后新建一个UITableView的类,在新建的类的.m文件里面设置行数,设置组,绘制单元格,需要更改Controller的初始状态。

 

转载于:https://my.oschina.net/1861/blog/710764

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值