[原]列表的二级展开功能。在原生UITableView上扩展实现。

本文介绍如何使用Objective-C和Swift为不同状态的UI元素创建自定义状态管理,通过设置和读取状态字典来动态更新UI组件,并实现状态之间的切换。包括初始化数据源、获取数据源大小、构建和选择UI组件等功能。

  还是直接贴代码了。

第一步:

@interface TodoViewController ()
{
    //声明一个数组 存放cell的信息
    NSMutableArray *grouparr;
}
@end

 

第二步:

//将cell的状态存入数组中
-(void)initDataSource
{
    NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];
    NSMutableDictionary *nameAndStateDic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];
    NSMutableDictionary *nameAndStateDic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];
    NSMutableDictionary *nameAndStateDic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];
    grouparr = [[NSMutableArray alloc] initWithObjects:nameAndStateDic1,nameAndStateDic2,nameAndStateDic3, nameAndStateDic4,nil];
}

  

第三步:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return grouparr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"TodoCell"]) {
        
        static NSString *cellID = @"cell";
        TodoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[TodoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            
        }
        NSData *data = dataArray[indexPath.row];
 
        [cell bindData:data];
        
        return cell;
    }
    else {
        
        static NSString *CellIdentifier = @"AttachedCell";
        
        AttachedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            cell = [[AttachedCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    
    return nil;
    
}

  

第四步:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击cell后 改变cell的颜色 渐变
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSIndexPath *path = nil;
    if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"MainCell"])
    {
        if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"MainCell"]) {
            path = [NSIndexPath indexPathForItem:(indexPath.row+1) inSection:indexPath.section];
        }
        else if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"AttachedCell"])
        {
            path = indexPath;
        }
    
        NSLog(@"现在是第%ld行",indexPath.row);
    
        if ([grouparr[indexPath.row][@"state"] boolValue] ) {
            // 关闭附加cell
            NSMutableDictionary *dd = grouparr[indexPath.row];
            NSString *name = dd[@"name"];
            NSMutableDictionary *nameAndStateDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",name,@"name",@"NO",@"state",nil];
            grouparr[(path.row-1)] = nameAndStateDic;
            [grouparr removeObjectAtIndex:path.row];
            NSLog(@"MainCell's grouparr:%@",grouparr);
            [_tableView beginUpdates];
            [_tableView deleteRowsAtIndexPaths:@[path]  withRowAnimation:UITableViewRowAnimationMiddle];
            [_tableView endUpdates];
        }
        else
        {
            // 打开附加cell
            NSMutableDictionary *dd = grouparr[indexPath.row];
            NSString *name = dd[@"name"];
        
            NSMutableDictionary *nameAndStateDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",name,@"name",@"YES",@"state",nil];

            grouparr[(path.row-1)] = nameAndStateDic;
        
            NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state",nil];
        
            [grouparr insertObject:nameAndStateDic1 atIndex:path.row];
            NSLog(@"AttachedCell's grouparr:%@",grouparr);
            [_tableView beginUpdates];
            [_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
            [_tableView endUpdates];
        }
    }
}

  

搞定收工!

转载于:https://www.cnblogs.com/pandas/p/4287831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值