UITableView 使用详解

一,常规使用

创建对像,设置代理

_table = [[UITableView alloc]initWithFrame: [UIScreen mainScreen].bounds];

_table.delegate = self;

_table.dataSource = self;

[_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellId"];

[self.view addSubview:_table];

代理回调

#pragma mark - UITableViewDelegate

//点击一行回调
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            break;
        }
        default:
            break;
    }
}

//某section 有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

//有多少section
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//sectionHeader高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

//返回cell
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell的重复利用
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId"];

    cell.textLabel.text = @"content";

    return cell;
}

二,使用遇到的问题

1. iOS 15中sectionHeader有个Padding,sectionView上面会多出一块

     全局处理一下

if (@available(iOS 15.0, *))
{
    [UITableView appearance].sectionHeaderTopPadding = 0;
}

2.关于sectionView

    UITableViewStylePlain,          // regular table view

    UITableViewStyleGrouped,        // sections are grouped together

    UITableViewStyleInsetGrouped  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos)  // grouped

默认情况下 grouped的sectionHeader不会悬浮。plain反之。.plain会出现多余的分割线,grouped不会。(好像可以通过其他方式修改,没试过)

3.间隔线问题

     1)设置间隔线

     _mTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

     

     类型有以下三种

     UITableViewCellSeparatorStyleNone //不显示分割线

     UITableViewCellSeparatorStyleSingleLine //单线(默认)(左边不到屏幕)

     UITableViewCellSeparatorStyleSingleLineEtched //内嵌线 (左边到屏幕)

     

     2)去掉单个cell 分瞎线

     [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, MAXFLOAT)];

     

     3)去掉多余的间隔线

     self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

     

4.cell点击效果

     默认点击一个cell后 cell的点击效果会一直存在,要想点击完后,点击效果跟着消失,

     在didSelectRowAtIndexPath:方法中

     调用[tableView deselectRowAtIndexPath:indexPath animated:NO];

5.cell重叠的现像

     如果想隐藏某个cell时把cell的高度设为0.0f的话, 这样会出现 cell重叠的现像。        cell.clipToBounds = YES;解决问题

6. tableViewHeaderView的使用(待续)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值