IOS开发-tableViewCell 的小demo,group分组。

这篇博客展示了如何在iOS开发中实现一个带有分组的TableViewCell的简单示例。主要内容包括设置左侧、右侧和顶部数组以组织数据,并通过代码实现Cell的锁定功能。

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

效果图:   

直接上代码:

                        //
//  ViewController.m
//  MyAccount
//
//  Created by chenkai on 15/7/30.
//  Copyright (c) 2015年 chenkai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"账户详情";

    self.view.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:239.0/255.0 blue:244.0/255.0 alpha:1];
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.tableFooterView=[[UIView alloc]init];
    tableView.dataSource=self;
    tableView.delegate=self;
    [self.view addSubview:tableView];
    _arrLeft0 = [NSMutableArray arrayWithObjects:@"",@"昵称",nil];
    _arrLeft1 = [NSMutableArray arrayWithObjects:@"实名认证",@"手机认证",@"身份认证", nil];
    _arrLeft2 = [NSMutableArray arrayWithObjects:@"重置登陆密码",@"重置支付密码",@"指纹", @"手势密码", nil];
    _arrRight0 = [NSMutableArray arrayWithObjects:@"",@"张起灵",nil];
    _arrRight1 = [NSMutableArray arrayWithObjects:@"张**",@"1515****111",@"1212**************12",nil];
    _arrTop = [NSMutableArray arrayWithObjects:@"",@"个人信息",@"密码管理",@"", nil];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}
#pragma mark 返回每组头标题名称
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    
//    UILabel *lab = [UILabel alloc];
//    lab.text = [_arrTop objectAtIndex:section];
//    lab.textColor = [UIColor redColor];
//    return lab.text;
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, self.view.frame.size.width, 40.0)] ;
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor grayColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:15];
    headerLabel.frame = CGRectMake(10.0, 0.0, self.view.frame.size.width, 40.0);
    
    headerLabel.text = [_arrTop objectAtIndex:section];
    [customView addSubview:headerLabel];
    return customView;
}

#pragma mark返回多少个section
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

#pragma mark返回行数,也就是section的元素
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    if (section == 1) {
        return 3;
    }
    if (section ==2) {
        return 4;
    }
    return 1;
}

#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //    声明静态字符串型对象,用来标记重用单元格
    static NSString *TableSampleIdentifier = @"MyCountTableViewCell";
    //    用MyCountTableViewCell表示需要重用的单元
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    cell.accessoryType = UITableViewCellAccessoryNone;
    NSUInteger row = [indexPath row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    //头像
    if (indexPath.section == 0) {
        cell.textLabel.text = [_arrLeft0 objectAtIndex:row];
        cell.detailTextLabel.text = [_arrRight0 objectAtIndex:row];
    if (row == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//加箭头

       
        UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 60, 60)];
        imgView.image = [UIImage imageNamed:@"1"];
        imgView.layer.cornerRadius = imgView.frame.size.width / 2;
        imgView.clipsToBounds = YES;
        [cell.contentView addSubview:imgView];

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-100, 18, 100, cell.frame.size.height)];
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        label.textAlignment = NSTextAlignmentLeft;
        [label setText:@"设置头像"];
        [label setTextColor:[UIColor orangeColor]];
        label.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:label];

    }
        if (row == 1) {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被点击
        }
    }
    //个人信息
    if (indexPath.section == 1) {
        cell.textLabel.text = [_arrLeft1 objectAtIndex:row];
        cell.detailTextLabel.text = [_arrRight1 objectAtIndex:row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //密码管理
    if (indexPath.section == 2) {
        cell.textLabel.text = [_arrLeft2 objectAtIndex:row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (row == 3) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            
            UIImage *img = [UIImage imageNamed:@"3"];
            if (_cellLock == 2) {
                img = [UIImage imageNamed:@"2"];
            }
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 60, 10, 50, 30)];
            imgView.image = img;
            imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            [cell.contentView addSubview:imgView];
        }
    }
    if (indexPath.section == 3) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [label setText:@"退出登录"];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label];
    }

    return cell;
}


#pragma mark 设置分组标题内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 40;
}

#pragma mark 设置每行高度(每行高度可以不一样)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 && indexPath.section ==0) {
        return 80;
    }
    return 50;
}

#pragma mark 点击行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld====",(long)indexPath.section); NSLog(@"%ld-----",(long)indexPath.row);
}

@end

在.h文件中需要定义几个常量:

{

    NSMutableArray *_arrLeft0;
    NSMutableArray *_arrLeft1;
    NSMutableArray *_arrLeft2;
    
    NSMutableArray *_arrRight0;
    NSMutableArray *_arrRight1;
    
    NSMutableArray *_arrTop;
    
    int  _cellLock;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值