點(diǎn)擊cell后展開和刪除 聯(lián)動
#import "ViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,CustomTableViewCellDelegate>
{
UITableView *_tableView;
NSMutableArray *_datas;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//因?yàn)橛袃蓚€索引,所以我們建一個數(shù)組
_datas = @[@0,@0].mutableCopy;
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
// NSLog(@"-->%@",[_tableView subviews]);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _datas.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.delegate = self;
if([_datas[indexPath.row] isEqual:@0]||[_datas[indexPath.row] isEqual:@1]){
//索引等于0和1的時候
cell.textLabel.text = @"項目";
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor grayColor];
}else{
//索引不等0和1的時候
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = @"開工······";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//判斷展開
if([_datas[indexPath.row]isEqual:@0]){//exp
NSMutableArray *indexPaths = [NSMutableArray array];
for (NSInteger i = 1; i<4; i++) {
[_datas insertObject:@2 atIndex:indexPath.row+1];
NSIndexPath *customIndexPath = [NSIndexPath indexPathForRow:(indexPath.row+i) inSection:0];
[indexPaths addObject:customIndexPath];
}
//在索引的地方插入,并展開動畫
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
#if 0
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[tableView reloadData];
});
});
#endif
_datas[indexPath.row] = @1;
return;
}
//判斷關(guān)閉(刪除)
if([_datas[indexPath.row]isEqual:@1]){//close
NSMutableArray *indexPaths = [NSMutableArray array];
for (NSInteger i = 1; i<4; i++) {
[_datas removeObjectAtIndex:indexPath.row+1];
NSIndexPath *customIndexPath = [NSIndexPath indexPathForRow:(indexPath.row+i) inSection:0];
[indexPaths addObject:customIndexPath];
}
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom];
_datas[indexPath.row] = @0;
return;
}
if(![_datas[indexPath.row]isEqual:@0] && ![_datas[indexPath.row]isEqual:@1]){
NSLog(@"*");
}
}
@end
效果如下:

Paste_Image.png
如果你的產(chǎn)品經(jīng)理已經(jīng)規(guī)定了label需要的顏色字體,我們在擴(kuò)###展的時候就不要把它暴露出去,要固化。我們寫一個擴(kuò)展。
新建CustomTableViewCell
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@protocol CustomTableViewCellDelegate <NSObject>
@optional
- (void)tableViewCell:(UITableViewCell*)cell Clicked:(NSIndexPath*)indexPath;
@end
@interface CustomTableViewCell : UITableViewCell
//@property (nonatomic,strong)UILabel *contentLabel; 接口暴露,沒有固化
@property (nonatomic,copy)NSString *contentString;
@property (nonatomic,weak)id <CustomTableViewCellDelegate> delegate;
@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
@interface CustomTableViewCell ()
{
UILabel *_contentLabel;
UIButton *_button;
}
@end
@implementation CustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_contentLabel = [UILabel new];
[self.contentView addSubview:_contentLabel];
_contentLabel.frame = CGRectMake(5, 5, 100, 30);
_contentLabel.backgroundColor = [UIColor redColor];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setTitle:@"button" forState:UIControlStateNormal];
_button.backgroundColor = [UIColor blueColor];
[_button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[_button addTarget:self action:@selector(buttonCliced:) forControlEvents:UIControlEventTouchUpInside];
_button.frame = CGRectMake(200, 5, 100, 20);
[self.contentView addSubview:_button];
}
return self;
}
- (void)buttonCliced:(UIButton*)sender{
CGPoint point = [sender convertPoint:CGPointZero toView:[self findTableView]];
//找到索引位置就可以傳值出去,可是沒有TableView怎么傳?
//我們用下面的那個findTableView自己寫的方法遞歸查找到UITableView
NSIndexPath *indexPath = [[self findTableView] indexPathForRowAtPoint:point];
if([self.delegate respondsToSelector:@selector(tableViewCell:Clicked:)]){
[self.delegate tableViewCell:self Clicked:indexPath];
}
// NSLog(@"--->%@",[self findTableView]);
}
- (UITableView*)findTableView{
UIResponder *responder = self;
//從cell出發(fā)往父視圖遞歸查找,直到找到UITableView
while (![responder isKindOfClass:[UITableView class]]) {
responder = responder.nextResponder;
}
return (UITableView*)responder;
}
- (void)setContentString:(NSString *)contentString{
_contentLabel.text = contentString;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
在ViewController.m中實(shí)現(xiàn)
- (void)tableViewCell:(UITableViewCell *)cell Clicked:(NSIndexPath *)indexPath{
NSLog(@"%ld",indexPath.row);
}
編譯運(yùn)行,結(jié)果如下:

Paste_Image.png
沒有暴露接口,也不需要填什么數(shù)據(jù),直接使用。