美文网首页iOS学习开发
iOS 表格Cell展开

iOS 表格Cell展开

作者: 观星 | 来源:发表于2016-06-15 16:09 被阅读451次
    • simpleCell:显示简单的信息,点击显示/隐藏 detailCell
    • detailCell:显示详细的信息,默认不显示

    以前也做过这个功能,但我忘了以前是怎么做的。好像是借助了一个dictionary来记住那些Cell的detailCell已经被展开。懒得去查阅以前的代码,重新开始也许会更快一些。

    simpleCell和detailCell之间的联系是非常紧密,它们需要放置在一起。同时,这两者之间的变化不应该影响到其他的cell,这两个又需要独立出来。对于tableView而言,这种关系,不就是section么。

    每一组simpleCell和detailCell都归为一个section,需要展开/收缩时,更新相应的section就可以,其他的不会影响到内容。

    这样感觉好像比较简单。

    //
    //  CTQProjectListViewController.m
    //  CTQProject
    //
    //  Created by wangxuefeng on 16/6/15.
    //  Copyright © 2016年 code. All rights reserved.
    //
    
    #import "CTQProjectListViewController.h"
    #import "CTQProject.h"
    #import "CTQProjectSimpleCell.h"
    #import "CTQProjectDetailCell.h"
    
    @interface CTQProjectListViewController ()
    
    @property (strong, nonatomic) NSArray *dataSource;
    
    @end
    
    @implementation CTQProjectListViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        CTQProject *p0 = [CTQProject new];
        
        p0.open = YES;
        
        CTQProject *p1 = [CTQProject new];
        
        p1.open = NO;
        
        CTQProject *p2 = [CTQProject new];
        
        p2.open = NO;
        
        self.dataSource = @[p0, p1, p2];
        
        [self.tableView reloadData];
    }
    
    #pragma mark - UITableViewDelegate & UITableViewSource
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        CTQProject *project = self.dataSource[indexPath.section];
        
        if (indexPath.row == 0) {
            
            project.open = !project.open;
            
            [self.tableView beginUpdates];
            
            [self.tableView reloadSection:indexPath.section withRowAnimation:UITableViewRowAnimationAutomatic];
            
            [self.tableView endUpdates];
        }   
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        CTQProject *project = self.dataSource[section];
        
        return project.isOpen ? 2 : 1;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        
        return self.dataSource.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        CTQProject *project = self.dataSource[indexPath.section];
        
        if (indexPath.row == 0) {
            CTQProjectSimpleCell *cell = [CTQProjectSimpleCell cellForTableView:tableView];
            
            [XFLineHelper addBottomLineToView:cell];
            
            return cell;
        } else {
            CTQProjectDetailCell *cell = [CTQProjectDetailCell cellForTableView:tableView];
            
            [XFLineHelper addBottomLineToView:cell];
            
            return cell;
        }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
            return [CTQProjectSimpleCell cellHeight];
        } else {
            return [CTQProjectDetailCell cellHeight];
        }
    }
    
    @end

    相关文章

      网友评论

        本文标题:iOS 表格Cell展开

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