tableview实现静态表格(纯代码)组头组尾实现

本文介绍如何使用Swift通过代理方法配置UITableView的样式与内容,包括分组形式、单元格样式及自定义视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们通过tableviewController创建tableview,如果只是实现三个代理方法。我们看下效果

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return 1;
    if (section == 1) return 3;
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = @"微信号";
            cell.detailTextLabel.text = @"15901282931";
            cell.accessoryType = UITableViewCellAccessoryNone;
            return cell;
        case 1:
            if (indexPath.row == 0) {
                cell.textLabel.text = @"QQ号";
                cell.detailTextLabel.text = @"342934426";
                return cell;
            }else if (indexPath.row == 1) {
                cell.textLabel.text = @"手机号";
                cell.detailTextLabel.text = @"15901282931";
                return cell;
            }else  {
                cell.textLabel.text = @"邮箱地址";
                cell.detailTextLabel.text = @"342934426@qq.com";
                return cell;
            }
        default:
            if (indexPath.row == 0) {
                cell.textLabel.text = @"微信密码";
                return cell;
            }else if (indexPath.row == 1) {
                cell.textLabel.text = @"账号保护";

                BOOL isProtect = NO;
                //创建 ImageView
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 7, 30, 30)];
                imageView.image = [UIImage imageNamed:isProtect ? @ "ProfileLockOn" : @"ProfileLockOff"];
                //创建 Label
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(275, 2, 100, 40)];
                label.text = isProtect ? @"以保护" : @"未保护";
                label.font = [UIFont systemFontOfSize:14];
//                label.textAlignment = NSTextAlignmentCenter;

                [cell.contentView addSubview:imageView];
                [cell.contentView addSubview:label];

                return cell;
            }else  {
                cell.textLabel.text = @"微信安全中心";
                cell.detailTextLabel.text = @"342934426@qq.com";
                return cell;
            }
    }
}

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

可以确定默认代码方式,tableview就是分组的形式。下面我们强行改变tableview的样式,再看一下效果

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
}

这里写图片描述
下面再通过,组头组尾的设置依然能够实现第一种效果:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return  nil;

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section != 2) return nil;
    UIView *myView = [[UIView alloc]init];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.tableView.frame.size.width - 20, 40)];
    label.text = @"大家快来送积分卡的数量咖啡就死定了疯狂就是对方的身份大家快来送积分卡的数量咖啡就死";
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.numberOfLines = 2;
    [myView addSubview:label];
    return myView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 20;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值