iOS 表格Cell展開(kāi)

  • simpleCell:顯示簡(jiǎn)單的信息,點(diǎn)擊顯示/隱藏 detailCell
  • detailCell:顯示詳細(xì)的信息,默認(rèn)不顯示

以前也做過(guò)這個(gè)功能,但我忘了以前是怎么做的。好像是借助了一個(gè)dictionary來(lái)記住那些Cell的detailCell已經(jīng)被展開(kāi)。懶得去查閱以前的代碼,重新開(kāi)始也許會(huì)更快一些。

simpleCell和detailCell之間的聯(lián)系是非常緊密,它們需要放置在一起。同時(shí),這兩者之間的變化不應(yīng)該影響到其他的cell,這兩個(gè)又需要獨(dú)立出來(lái)。對(duì)于tableView而言,這種關(guān)系,不就是section么。

每一組simpleCell和detailCell都?xì)w為一個(gè)section,需要展開(kāi)/收縮時(shí),更新相應(yīng)的section就可以,其他的不會(huì)影響到內(nèi)容。

這樣感覺(jué)好像比較簡(jiǎn)單。

//
//  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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類(lèi)似...
    liudhkk閱讀 9,283評(píng)論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,103評(píng)論 4 61
  • 我們?cè)谏弦黄锻ㄟ^(guò)代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識(shí),本文將在上文的基礎(chǔ)上,利用sto...
    啊世ka閱讀 1,640評(píng)論 2 7
  • 你有多久沒(méi)見(jiàn)過(guò) 冬天 白雪鋪滿大地 只有兔子的足跡 從小 我便生活在這里 一個(gè)簡(jiǎn)單幸福的地方 你說(shuō) 唯有那山后的世...
    不經(jīng)之談閱讀 268評(píng)論 0 4
  • 第6條:理解“屬性”這一概念 1. 屬性的概念 “屬性”(property)是Objective-C的一項(xiàng)特性,用...
    dibadalu閱讀 400評(píng)論 5 1

友情鏈接更多精彩內(nèi)容