iOS Cell上实现简单的分组(类似与QQ分组)

1.创建一个工程,做一个表视图,并建一个自定义的Cell类



2.

//

//  MainTableViewController.h

//  QQ分组

//

//  Created by 黄权浩 on 14-10-26.

//  Copyright (c) 2014 黄权浩. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface MainTableViewController : UITableViewController

{

@private

    BOOL _isCell;//来区别是否点击了这个Cell

}

@end


//

//  MainTableViewController.m

//  QQ分组

//

//  Created by 黄权浩 on 14-10-26.

//  Copyright (c) 2014 黄权浩. All rights reserved.

//


#import "MainTableViewController.h"

#import "MyCell.h"//自定义Cell


@interface MainTableViewController ()


@end


@implementation MainTableViewController


- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];


    self.tableView.frame = CGRectMake(0, 60, 320, self.view.bounds.size.height-60);

    

    self.tableView.separatorStyle=UITableViewCellSelectionStyleNone;

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;//在这里返回你的分组数

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSLog(@"%ld",(long)section);

    NSLog(@"_isCell:%d",_isCell);

    switch (section) {

        case 0:

            if (_isCell) {

                return 4;

            }else {

                return 1;

            }

            

            break;

            

        default:

            break;

    }

    return 0;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellIdentifier = @"mycell";

    static NSString *cellIdentifier2 = @"mycell2";

    NSLog(@"section:%ld",(long)indexPath.section);

    NSLog(@"row:%ld",(long)indexPath.row);

    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];

    switch ([indexPath section]) {

        case 0:

            switch ([indexPath row]) {

                case 0:

                    if (cell == nil) {

                        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

                        

                    }

                    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];

                    return cell;

                    break;

                    

                default:

                    if (cell2 == nil) {

                        cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2];

                        

                    }

                    cell2.textLabel.text = @"12345";

                    return cell2;

                    

                    break;

            }

            break;

            

        default:

            break;

    }

    return cell;

}



#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];

    NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]];

    NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]];

    NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]];

    NSArray *array = [NSArray arrayWithObjects:path0, path1, path2, nil];

    

    switch ([indexPath section]) {

        case 0:

            switch ([indexPath row]) {

                case 0:

                    if ([cell isOpen]) {

                        [cell setClosed];

                        _isCell = [cell isOpen];

                        [tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];

                        

                    }else {

                        // 开启  isopenyes

                        [cell setOpen];

                        // _boolSection1 设置为yes

                        _isCell = [cell isOpen];

                        

                        [tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];

                    }

                    break;

                    

                default:

                    NSLog(@"default");

                    cell.textLabel.text = @"default";

                    break;

            }

            break;

            

        default:

            break;

    }

}

//

//  MyCell.h

//  QQ分组

//

//  Created by 黄权浩 on 14-10-26.

//  Copyright (c) 2014 黄权浩. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface MyCell : UITableViewCell

@property (nonatomic) BOOL isOpen;


- (void) setOpen;

- (void) setClosed;

@end


//

//  MyCell.m

//  QQ分组

//

//  Created by 黄权浩 on 14-10-26.

//  Copyright (c) 2014 黄权浩. All rights reserved.

//


#import "MyCell.h"


@implementation MyCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 30)];

        l.backgroundColor = [UIColor redColor];

        l.tag = 101;

        [self.contentView addSubview:l];

    }

    return self;

}


- (void) setOpen

{

    [self setIsOpen:YES];

}

- (void) setClosed

{

    [self setIsOpen:NO];

}


- (void)awakeFromNib

{

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end

 

3.实现效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黄权浩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值