iOS中Cell的展开和收起

作者: MR小锦 | 来源:发表于2017-05-22 15:40 被阅读1553次

    首先,先上图,让大家看看效果

    Simulator Screen Shot 2017年5月22日 下午1.43.37.png

    相信大家对于TableViewd数据的设置都熟悉,这方面就不多说的,重点的还是来看:

    1.如何实现cell的展开和收起的效果

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
        
        
        currentRow = indexPath.row;
        
        NSDictionary *sectionDic = self.dataSource[indexPath.section];
        NSArray *cellArray = sectionDic[@"sub"];
        
        //cell当前的数据
        NSDictionary *cellData = cellArray[indexPath.row];
        
        NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];
        CellModel *chapterModel = [self.cellOpen valueForKey:key];
        
        chapterModel.isShow = !chapterModel.isShow;
        
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    

    当用户点击到某一个cell时候,需要判断cell是否是展开状态,如果张开或者收起就调用

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    

    让cell的section能够重新加载刷新;

    2.如何增加cell的某一个Section的row

    2.1设置好对用是否展开row的高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSDictionary *sectionDic = self.dataSource[indexPath.section];
        NSArray *cellArray = sectionDic[@"sub"];
        //cell当前的数据
        NSDictionary *cellData = cellArray[indexPath.row];
        
        NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];
        CellModel *model = [self.cellOpen valueForKey:key];
        if (model.isShow) {
            return (model.pois.count+1)*60;
        } else {
            return 60;
        }
        
    }
    
    2.2 返回对应的section的row
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        BOOL sectionStates = [self.sectionOpen[section] boolValue];
        
        if(sectionStates)
        {
            //数据决定显示多少行cell
            NSDictionary *sectionDict = self.dataSource[section];
            //section决定cell的数据
            NSArray *cellArray = sectionDict[@"sub"];
            return cellArray.count;
            
        }
        else
        {
            //section是收起的时候
            return 0;
        }
    }
    
    好了,说了那么多,估计大家还是喜欢看demo,以下是demo的链接:https://github.com/xiaojin1123/SectionOpenAndClose.git
    欢迎大家提出自己看法!

    相关文章

      网友评论

        本文标题:iOS中Cell的展开和收起

        本文链接:https://www.haomeiwen.com/subject/dmwbxxtx.html