UITableView使用笔记

UITableView的基本概念

UITableView继承于UIScrollView,所以UITableView也可以使用UIScrollView的代理方法。在使用前记得添加UITableView的俩个协议<UITableViewDelegate, UITableViewDataSource>。

UITableView的懒加载

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        //代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorColor = UIColor.blueColor;
        //标识线
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        //cell注册
        [_tableView registerClass:[CellClass class] forCellReuseIdentifier:NSStringFromClass([CellClass class])];
    }
    return _tableView;
}

UITableView主要用到的有两种样式,UITableViewStylePlain可以起到让区头悬浮的作用。UITableViewStyleGrouped区头会随着cell滑动。

separatorStyle属性:设置cell之间的分隔线样式。

separatorColor属性:设置分隔线颜色。

showsVerticalScrollIndicatorshowsHorizontalScrollIndicator属性:设置表的水平、垂直滚动条是否显示。

UITableView的代理方法

@required 必须实现的

区里有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return rowCount;
}

cell的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
	CellClass *cell = [[CellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([CellClass class])];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.accessoryType = UITableViewCellAccessoryNone;
	return cell;
}

selectionStyle属性:设置cell选中样式。

UITableViewCellSelectionStyleNone
UITableViewCellSelectionStyleBlue
UITableViewCellSelectionStyleGray
UITableViewCellSelectionStyleDefault

accessoryType属性:设置cell右边指示样式。

UITableViewCellAccessoryNone 没有
UITableViewCellAccessoryDisclosureIndicator 箭头
UITableViewCellAccessoryDetailDisclosureButton 感叹号和箭头
UITableViewCellAccessoryCheckmark 对勾
UITableViewCellAccessoryDetailButton 感叹号

在这里插入图片描述

@optional 非必须实现的

表里有多少区(默认为1)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return sectionCount;
}

区头高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return headerHeight;
}

区头View

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    return headerView;
}

区尾高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return footerHeight;
}

区尾View

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}

行点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"行点击");
}

表的索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *array = @[@"A",@"B",@"C",@"D"];
    return array;
}

UITableView 常用方法

刷新tableView数据

[self.tableView reloadData];

解决iOS15以后区头留白的问题

if (@available(iOS 15.0, *)) {
    self.tableView.sectionHeaderTopPadding = 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值