IOS-37-TableView及Cell的应用和设置详解

本文详细介绍了 iOS 开发中 TableView 的常见配置与使用方法,包括 cell 样式设置、分区间距、行高及数量调整等,并提供了消除底部多余 cell 和调整顶部间距的具体实现。

Tableview是iOS开发中最为普遍的应用组件,可以吐槽的说没有一个应用不调用tableview。所以你得熟练的掌握它,在此我总结了一些常用的设置供大家参考:
1.cellForRowAtIndexPath方法调用(用于设置cell样式)

//设置cell具体内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PersonInfoCell";
    //初始化cell并指定类型
    TLDPersonInfoCell *cell = (TLDPersonInfoCell *)[self.personTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *cellnNibs = [[NSBundle mainBundle] loadNibNamed:@"TLDPersonInfoCell" owner:self options:nil];
        if (cellnNibs) {
            cell =  (TLDPersonInfoCell *)[cellnNibs firstObject];
        }
    }

//    [self.personTableView setSeparatorInset:(UIEdgeInsetsMake(0, 0, 0, 0))];//设置tableview里的cell自带分割线充满屏幕

    //自定义并添加cell分割线样式
    UIView *Cellbtmline=[[UIView alloc]initWithFrame:CGRectMake(0, 39.5, kMAIN_SCREEN_WIDTH, 0.5)];
    Cellbtmline.backgroundColor=newLineColor;
    UIView *Celltopline=[[UIView alloc]initWithFrame:CGRectMake(0,0, kMAIN_SCREEN_WIDTH, 0.5)];
    Celltopline.backgroundColor=newLineColor;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell自带右边箭头
 //   _personTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//将cell设置为不需要分割线
    //对应各自的分区
    switch (indexPath.section) {
        case 0:
        {
         }
            break;
        defaultbreak;
}
return cell;

2.其他几个常用方法:

//分区间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    return 5.0;
}

//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

//各分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
        case 1:
            return 5;
        default:
            return 2;
    }
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==2&&indexPath.row==1) {
        return 60;
    }

    if(indexPath.section==0)
    {
        return 70;
    }else
    {
        return 40;
    }
}

3.设置tableview底部多余cell不显示
(1).tableview为Plain模式:

_myAccountTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

(2).tableview为Grouped模式:
去掉顶部多余的间距:

CGRect frame=CGRectMake(0, 0, 0, CGFLOAT_MIN);
    _myAccountTableView.tableHeaderView=[[UIView alloc]initWithFrame:frame];

4.效果如下:
这里写图片描述

模拟器和真机运行的cell效果不太一样,建议真机运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IOT_Elon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值