UITableView

1、什么是TableView

* 就是在APP中看到的列表数据

* 在iOS中,要实现展示列表数据,最常用的做法就是使用UITableView

* UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳

2、 TableView的两种样式

UITableViewStylePlain (例如:汽车之家主界面)

UITableViewStyleGroupe (例如:微信的发现界面)

3、 如何展示数据

* UITableView需要一个数据源(dataSource)来显示数据
 
*UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等
#pragma mark - <UITableViewDataSource>
/**
 * 告诉tableView一共有多少组
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return   2; // 随便写的
}

/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) return 2;
    if (section == 1) return 5;
    return 0;
}

/**
 * 告诉tableView每一行显示什么内容(tableView的每一行都是UITableViewCell)
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if (indexPath.section == 0) { // 第0组
        if (indexPath.row == 0) { // 第0组第0行
            cell.textLabel.text = @"通用";
        } else if (indexPath.row == 1) { // 第0组第1行
            cell.textLabel.text = @"隐私";
        }
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"其他数据-%zd组%zd行", indexPath.section, indexPath.row];
    }
    return cell;
}

 

*没有设置数据源的UITableView只是个空壳
 
*凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

 

TableView的常见属性

//1. 修改tableView的行高
    self.tableView.rowHeight = 100;

    // 2.组头组尾的高
    self.tableView.sectionHeaderHeight = 55;
    self.tableView.sectionFooterHeight = 22;

    // 3.设置整个tablView的头部/尾部视图
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoDark];

    // 4.设置我们分割线颜色(clearColor相当于取消系统分割线)
    //self.tableView.separatorColor = [UIColor clearColor];

    // 5.设置分割线样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        // 设置索引条内部文字颜色为不纯洁颜色
    self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    // 设置索引条背景颜色为纯洁颜色
    self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];

 TableView常见的代理方法:

// 当选中index行cell时候会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


// 当取消选中cell时候调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath


// 返回对应行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath


// 返回对应组的组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section


// 返回对应组头试图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

 

转载于:https://www.cnblogs.com/xingdianjun/p/4959775.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值