iOS開發(fā):UITableView嵌套UICollectionView結合使用

效果GIF.gif

在UITableViewCell中嵌套UICollectionViewCell齿拂,效果圖中玖绿,上面兩個cell和下面幾個cell是不同類型的,像這種效果蛇受,上面一部分也可以在UITableViewCell中通過自定義視圖實現(xiàn)雹顺,不過我個人覺得還是這種嵌套的方式更加的簡單些丹墨;

RootViewController.h的代碼:

#import <UIKit/UIKit.h>
#import "RootTableCell.h"

@interface RootViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, RootCellDelegate>

@end

RootViewController.m的代碼:

#import "RootViewController.h"
#import "MyTableCell.h"

#define K_T_Cell @"t_cell"
#define K_C_Cell @"c_cell"
@interface RootViewController ()

@property (nonatomic, strong) NSArray *dataAry;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *dicH;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"嵌套使用";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.dataAry = @[@[@"1元", @"2元", @"3元", @"4元", @"5元", @"6元"],
                     @[@"1元", @"2元", @"3元", @"4元", @"10元", @"20元", @"30元", @"40元", @"11元", @"22元", @"33元"]];
    [self.view addSubview:self.tableView];
}

#pragma mark ====== UITableViewDelegate ======
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataAry.count + 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dicH[indexPath]) {
        NSNumber *num = self.dicH[indexPath];
        return [num floatValue];
    } else {
        return 60;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.dataAry.count) {
        [tableView registerClass:[RootTableCell class] forCellReuseIdentifier:K_C_Cell];
        RootTableCell *cell = [tableView dequeueReusableCellWithIdentifier:K_C_Cell forIndexPath:indexPath];
        cell.delegate = self;
        cell.indexPath = indexPath;
        cell.dataAry = self.dataAry[indexPath.row];
        return cell;
    } else {
        [tableView registerClass:[MyTableCell class] forCellReuseIdentifier:K_T_Cell];
        MyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:K_T_Cell forIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

#pragma mark ====== RootTableCellDelegate ======
- (void)updateTableViewCellHeight:(RootTableCell *)cell andheight:(CGFloat)height andIndexPath:(NSIndexPath *)indexPath {
    if (![self.dicH[indexPath] isEqualToNumber:@(height)]) {
        self.dicH[indexPath] = @(height);
        [self.tableView reloadData];
    }
}

//點擊UICollectionViewCell的代理方法
- (void)didSelectItemAtIndexPath:(NSIndexPath *)indexPath withContent:(NSString *)content {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:content delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    [alertView show];
}

#pragma mark ====== init ======
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableDictionary *)dicH {
    if (!_dicH) {
        _dicH = [[NSMutableDictionary alloc] init];
    }
    return _dicH;
}

@end

主要是UITableViewCell中的實現(xiàn) RootTableCell.h文件

#import <UIKit/UIKit.h>

@class RootTableCell;
@protocol RootCellDelegate <NSObject>

/**
 * 動態(tài)改變UITableViewCell的高度
 */
- (void)updateTableViewCellHeight:(RootTableCell *)cell andheight:(CGFloat)height andIndexPath:(NSIndexPath *)indexPath;


/**
 * 點擊UICollectionViewCell的代理方法
 */
- (void)didSelectItemAtIndexPath:(NSIndexPath *)indexPath withContent:(NSString *)content;
@end

@interface RootTableCell : UITableViewCell

@property (nonatomic, weak) id<RootCellDelegate> delegate;

@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) NSArray *dataAry;

@end

RootTableCell.m文件

#import "RootTableCell.h"
#import "RootCollectionCell.h"

#define K_Cell @"cell"
@interface RootTableCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat heightED;

@end

@implementation RootTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self.heightED = 0;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.collectionView];
        self.collectionView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.contentView.frame.size.height);
    }
    return self;
}

#pragma mark ====== UICollectionViewDelegate ======
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataAry.count == 0) {
        return 1;
    } else {
        return self.dataAry.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RootCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:K_Cell forIndexPath:indexPath];
    cell.textStr = self.dataAry[indexPath.row];
    [self updateCollectionViewHeight:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemAtIndexPath:withContent:)]) {
        [self.delegate didSelectItemAtIndexPath:indexPath withContent:self.dataAry[indexPath.row]];
    }
}

- (void)updateCollectionViewHeight:(CGFloat)height {
    if (self.heightED != height) {
        self.heightED = height;
        self.collectionView.frame = CGRectMake(0, 0, self.collectionView.frame.size.width, height);
        
        if (_delegate && [_delegate respondsToSelector:@selector(updateTableViewCellHeight:andheight:andIndexPath:)]) {
            [self.delegate updateTableViewCellHeight:self andheight:height andIndexPath:self.indexPath];
        }
    }
}

#pragma mark ====== init ======
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        CGFloat width = ([UIScreen mainScreen].bounds.size.width - 50) / 4;
        layout.itemSize = CGSizeMake(width, 60);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[RootCollectionCell class] forCellWithReuseIdentifier:K_Cell];
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (void)setDataAry:(NSArray *)dataAry {
//    [self.collectionView reloadData];
    self.heightED = 0;
    _dataAry = dataAry;
}

@end

至于里面的頁面,相對來說就很簡單了嬉愧,參考文章

Demo地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末带到,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌揽惹,老刑警劉巖被饿,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異搪搏,居然都是意外死亡狭握,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門疯溺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來论颅,“玉大人,你說我怎么就攤上這事囱嫩∈逊瑁” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵墨闲,是天一觀的道長今妄。 經(jīng)常有香客問我,道長鸳碧,這世上最難降的妖魔是什么盾鳞? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮瞻离,結果婚禮上腾仅,老公的妹妹穿的比我還像新娘。我一直安慰自己套利,他們只是感情好推励,可當我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肉迫,像睡著了一般验辞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上昂拂,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天受神,我揣著相機與錄音抛猖,去河邊找鬼格侯。 笑死,一個胖子當著我的面吹牛财著,可吹牛的內容都是我干的联四。 我是一名探鬼主播,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼撑教,長吁一口氣:“原來是場噩夢啊……” “哼朝墩!你這毒婦竟也來了?” 一聲冷哼從身側響起伟姐,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤收苏,失蹤者是張志新(化名)和其女友劉穎亿卤,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹿霸,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡排吴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了懦鼠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钻哩。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖肛冶,靈堂內的尸體忽然破棺而出街氢,到底是詐尸還是另有隱情,我是刑警寧澤睦袖,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布珊肃,位于F島的核電站,受9級特大地震影響扣泊,放射性物質發(fā)生泄漏近范。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一延蟹、第九天 我趴在偏房一處隱蔽的房頂上張望评矩。 院中可真熱鬧,春花似錦阱飘、人聲如沸斥杜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蔗喂。三九已至,卻和暖如春高帖,著一層夾襖步出監(jiān)牢的瞬間缰儿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工散址, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留乖阵,地道東北人。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓预麸,卻偏偏與公主長得像瞪浸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子吏祸,可洞房花燭夜當晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內容