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 {
// 开启 isopen为yes
[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.实现效果