点击cell,在cell下方创建一个新的cell,再次点击,删除该cell

初始化数组:

    NSDictionary *dic =@{@"Cell":@"MainCell",@"isAttached":@(NO)};

    NSArray * array =@[dic,dic,dic,dic,dic,dic];

    

    self.dataArray = [[NSMutableArrayalloc]init];

    self.dataArray = [NSMutableArrayarrayWithArray:array];


对tableview数据源进行配置

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

{

    

    

    if ([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"MainCell"])

    {

        

        staticNSString *CellIdentifier = @"MainCell";

        

        TEWdocumentViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

        

        

        if (cell ==nil) {

            cell = [[TEWdocumentViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

            

            cell.selectionStyle =UITableViewCellSelectionStyleGray;

        }

        cell.titleLabel.text =self.documentNames[indexPath.row];

        cell.titleLabel.textColor = [UIColorblackColor];

        cell.detailLabel.text =@"2015-03-06 13:43";

        cell.detailLabel.textColor = [UIColorgrayColor];

        cell.titleImageView.image = [UIImageimageNamed:kIconAlert];

        [cell.controlButtonaddTarget:selfaction:@selector(controlButtonTapped:)forControlEvents:UIControlEventTouchUpInside];

        cell.accessoryType =UITableViewCellAccessoryNone;

                    return cell;

        

    }elseif([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"AttachedCell"]){

        

        staticNSString *CellIdentifier = @"AttachedCell";

        

        TewMoreViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

        

        

        if (cell ==nil) {

            cell = [[TewMoreViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

            

            cell.selectionStyle =UITableViewCellSelectionStyleNone;

        }

        

        return cell;

        

    }

    

    returnnil;

}


- (void)controlButtonTapped:(id)sender

{

     NSIndexPath *path =nil;

    TEWdocumentViewCell *cell = (TEWdocumentViewCell *)[[sendersuperview] superview];

    NSIndexPath *indexPath = [_tableViewindexPathForCell:cell];

    

    if ([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"MainCell"]) {

        path = [NSIndexPathindexPathForItem:(indexPath.row+1)inSection:indexPath.section];

    }else{

        path = indexPath;

    }

    

    if ([[self.dataArray[indexPath.row]objectForKey:@"isAttached"]boolValue]) {

        // 关闭附加cell

        NSDictionary * dic =@{@"Cell":@"MainCell",@"isAttached":@(NO)};

        self.dataArray[(path.row-1)] = dic;

        [self.dataArrayremoveObjectAtIndex:path.row];

        

        [self.tableViewbeginUpdates];

        [self.tableViewdeleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

        [self.tableViewendUpdates];

        

    }else{

        // 打开附加cell

        NSDictionary * dic =@{@"Cell":@"MainCell",@"isAttached":@(YES)};

        self.dataArray[(path.row-1)] = dic;

        NSDictionary * addDic =@{@"Cell":@"AttachedCell",@"isAttached":@(YES)};

        [self.dataArrayinsertObject:addDic atIndex:path.row];

        [self.tableViewbeginUpdates];

        [self.tableViewinsertRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationMiddle];

        [self.tableViewendUpdates];

        

    }

    

}



若要求一次只显示一个附加cell的话,则在动作控制处设置

- (void)controlButtonTapped:(id)sender

{

     NSIndexPath *path = nil;

    TEWdocumentViewCell *cell = (TEWdocumentViewCell *)[[sender superview] superview];

    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];

    

    if ([[self.dataArray[indexPath.row] objectForKey:@"Cell"] isEqualToString:@"MainCell"]) {

        path = [NSIndexPath indexPathForItem:(indexPath.row+1) inSection:indexPath.section];

    }else{

        path = indexPath;

    }

    if ([[self.dataArray[indexPath.row] objectForKey:@"isAttached"] boolValue]) {

        // 关闭附加cell

        NSDictionary * dic = @{@"Cell": @"MainCell",@"isAttached":@(NO)};

        self.dataArray[(path.row-1)] = dic;

        [self.dataArray removeObjectAtIndex:path.row];

        [self.tableView beginUpdates];

        [self.tableView deleteRowsAtIndexPaths:@[path]  withRowAnimation:UITableViewRowAnimationMiddle];

        [self.tableView endUpdates];

    }else{

        // 打开附加cell

        if ([[self.dataArray[self.index.row] objectForKey:@"isAttached"] boolValue]) {

            // 关闭上一个附加cell

            NSDictionary * dic = @{@"Cell": @"MainCell",@"isAttached":@(NO)};

            self.dataArray[(self.index.row-1)] = dic;

            [self.dataArray removeObjectAtIndex:self.index.row];


            [self.tableView beginUpdates];

            [self.tableView deleteRowsAtIndexPaths:@[self.index]  withRowAnimation:UITableViewRowAnimationMiddle];

            [self.tableView endUpdates];

        }

        

        if(indexPath.row > self.index.row){

            path = indexPath;

        }

        // 打开附加cell

        NSDictionary * dic = @{@"Cell": @"MainCell",@"isAttached":@(YES)};

        self.dataArray[(path.row-1)] = dic;

        NSDictionary * addDic = @{@"Cell": @"AttachedCell",@"isAttached":@(YES)};

        [self.dataArray insertObject:addDic atIndex:path.row];

        [self.tableView beginUpdates];

        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

        [self.tableView endUpdates];

        self.index = path;

    }

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值