ios-表视图创建

 1、创建一个表视图的两种样式

        UITableViewStylePlain       ----->  平铺的效果

        UITableViewStyleGrouped     ----->  分组的效果

  2、表视图的结构

a) 表视图由头部是视图,尾部视图,中间由一连串的单元格组成

b) 表视图的头部由tableHeaderView属性设置,尾部由tableFooterView属性设置

c) 分组表格由一连串的section视图组成


.m文件中

#import "ViewController.h"

//1. 实现UITableViewDataSource协议,如果不实现,就无法加载数据

@interface ViewController () <UITableViewDataSource>


@property(nonatomic, strong) NSArray *fNames;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

        

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 200, 500) style:UITableViewStylePlain];

    

    // 设置数据源代理:如果不设置,数据就无法显示

    tableView.dataSource = self;

    

    // 添加到视图中

    [self.view addSubview:tableView];

    

    // 初始化字体数组

    _fNames = [UIFont familyNames];

    

}


#pragma mark - 返回单元格的分组个数

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

    return 1;

}


#pragma mark - 必须实现的数据协议

//2. 返回分组的单元格个数

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section {

    

    //返回的单元格的个数 ----> 字体的个数

    return _fNames.count;

}



- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    //1) 创建一个单元格

    /**

     UITableViewCellStyleDefault    //默认

     UITableViewCellStyleValue1     //字体改变

     UITableViewCellStyleValue2     //蓝色字体

     UITableViewCellStyleSubtitle   //自适应

     */

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    

    //2) 设置单元格的内容:indexPath.section ------> 代表分组   

    //                  indexPath.row --------> 代表行

    cell.textLabel.text = [NSString stringWithFormat:@"组:%ld, 行:%ld", indexPath.section, indexPath.row];

    

    //3) 设置字体内容

    cell.textLabel.text = _fontNames[indexPath.row];

    

    //4) 设置字体样式

    cell.textLabel.font = [UIFont fontWithName:_fontNames[indexPath.row] size:20];

    

    return cell;

    

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值