iOS开发:UITableView简单介绍

iOS开发:UITableView简单介绍
一、UITableView概述
 
 1、继承自UIScrollView,所以可以滚动。
  2、表视图的每一条数据都是显示在UITableViewCell对象中。
  3、表视图可以分区显示数据,UITableView声明了一个NSIndexPath的类别,主要用来标识当前cell的在tableView中的位置,该类别有sectionrow两个属性,前者标识当前cell处于第几个section中,后者代表在该section中的第几行
每个分区称为一个section,每一行称为一个row,编号都是从0开始。

因为UITableView很常用,在iOS视图里很重要,需求也很大,系统给定很多实现的方法,所以需要签订两个协议来进行相关操作:

<UITableViewDataSource, UITableViewDelegate>

    _tableView.dataSource = self;

    _tableView.delegate = self;

TableView的数据源UITableViewDataSource

TableView的委托UITableViewDelegate

     dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。

  delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。

二、具体实现

//初始化tableView

 _tableView  =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];

style有两种:UITableViewStyleGrouped 和 UITableViewStylePlain



 //初始化数组

    _dataSourceArray = [[NSMutableArray alloc]initWithObjects:@"宋江"@"卢俊义"@"吴用"@"公孙胜"@"关胜"@"林冲"@"秦明"@"呼延灼"@"花荣"@"柴进"@"李应"@"朱仝"@"鲁智深"@"武松"@"董平"@"张清"@"杨志"@"徐宁"@"索超"@"戴宗"@"刘唐"@"李逵"@"史进"@"穆弘"@"雷横"@"李俊" ,@"阮小二"@"张衡"@"阮小五"@"张顺"@"阮小七"@"杨雄"@"石秀"@"解珍"@"解宝"@"燕青"nil];

协议里必须实现的两个方法:

#pragma mark cell的个数, 即行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return _dataSourceArray.count;

}


#pragma mark cell的样式,即具体内容

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

//设置重用池里cell的标示:

    static NSString *identifier = @"Kevin";

//首先去重用池找是否有可利用的cell:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {//if(cell == nil),新建一个cell

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

        *说明:initWithStyle有四种方式

    }

//对cell的相关操作


    //cell显示内容(应该是数组里的内容)

    cell.textLabel.text = [_dataSourceArray objectAtIndex:indexPath.row];

    cell.imageView.image = [UIImage imageNamed:@"iconfont-dianhua.png"];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.detailTextLabel.text = @"detailTextLabel";

 for (NSInteger i = 0; i < 6; i++) {

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100 + 35 * i, 0, 30, 20)];

        label.backgroundColor = [UIColor greenColor];

        label.text = [NSString stringWithFormat:@"%ld", i];

        [cell.contentView addSubview:label];

    }

    

    return cell;

}

结果如图:

#pragma mark设置cell的高度

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

        return 80;


}

#pragma mark 点击cell时触发的方法(调转到下一界面)

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    SecondViewController *second = [[SecondViewController alloc]init];

    [self.navigationController pushViewController:second animated:YES];

}










  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值