iOS 表格Cell展开

  • 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,977评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,429评论 4 61
  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 5,422评论 2 7
  • 你有多久没见过 冬天 白雪铺满大地 只有兔子的足迹 从小 我便生活在这里 一个简单幸福的地方 你说 唯有那山后的世...
    不经之谈阅读 1,724评论 0 4
  • 第6条:理解“属性”这一概念 1. 属性的概念 “属性”(property)是Objective-C的一项特性,用...
    dibadalu阅读 2,839评论 5 1

友情链接更多精彩内容