国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

iOS實(shí)現(xiàn)折疊單元格

瀏覽:63日期:2022-09-17 08:32:02

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)折疊單元格的具體代碼,供大家參考,具體內(nèi)容如下

思路

點(diǎn)擊按鈕或cell單元格來(lái)進(jìn)行展開(kāi)收縮, 同時(shí)使用一個(gè)BOOL值記錄單元格展開(kāi)收縮狀態(tài)。根據(jù)BOOL值對(duì)tableView的高度和button的image進(jìn)行實(shí)時(shí)變更。

注意點(diǎn):

在執(zhí)行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath( 點(diǎn)擊當(dāng)前單元格)方法時(shí),收縮單元格,顯示當(dāng)前點(diǎn)擊的單元格的內(nèi)容。這一步驟的實(shí)現(xiàn)是對(duì)存儲(chǔ)單元格內(nèi)容的可變數(shù)組進(jìn)行更改。

代碼

//ViewController.h 中#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property UITableView *tableView;@property UIButton *button; @property NSMutableArray *imageViewArr; @property NSMutableArray *labelArr; @property BOOL select; //記錄單元格展開(kāi)收縮狀態(tài)@end

//ViewController.m 中#import 'ViewController.h'#import 'ViewTableViewCell.h'#import 'Masonry.h'@interface ViewController () <UITableViewDelegate, UITableViewDataSource>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1]; _imageViewArr = [[NSMutableArray alloc] initWithObjects:@'1', @'2', @'3', @'4', @'5', nil]; _labelArr = [[NSMutableArray alloc] initWithObjects:@'發(fā)起群聊', @'添加朋友', @'掃一掃', @'收付款', @'幫助與反饋', nil]; _tableView = [[UITableView alloc] init]; [self.view addSubview:_tableView]; _tableView.frame = CGRectMake(100, 100, 130, 35); //以下使用Masonry對(duì)tableView進(jìn)行約束, 約束不是很規(guī)范 可忽略// [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {// make.height.mas_offset(self.view.frame.size.height * 0.0485);// make.width.mas_offset(self.view.frame.size.width * 0.335);// make.left.equalTo(self.view.mas_left).offset(self.view.frame.size.width * 0.6);// make.top.equalTo(self.view.mas_top).offset(self.view.frame.size.height * 0.046);//// }]; _tableView.delegate = self; _tableView.dataSource = self; [_tableView registerClass:[ViewTableViewCell class] forCellReuseIdentifier:@'cell']; _button = [UIButton buttonWithType:UIButtonTypeCustom]; [self.view addSubview:_button]; [_button mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_tableView.mas_right).offset(-28); make.top.equalTo(_tableView.mas_top).offset(4); make.height.mas_offset(self.view.frame.size.height * 0.0495 * 0.68); make.width.mas_offset(self.view.frame.size.width * 0.335 * 0.22); }]; [_button setImage:[UIImage imageNamed:@'shou'] forState:UIControlStateNormal]; [_button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside]; //默認(rèn)單元格為收縮 select為0 _select = 0;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //根據(jù)select的值來(lái)判斷收縮展開(kāi)狀態(tài),返回相應(yīng)的行數(shù) if(_select == 0) { return 1; } else { return 5; }}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@'cell' forIndexPath:indexPath]; cell.iimageView.image = [UIImage imageNamed:_imageViewArr[indexPath.row]]; cell.label.text = [NSString stringWithString:_labelArr[indexPath.row]]; return cell;}//點(diǎn)擊當(dāng)前單元格- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //記錄當(dāng)前單元格的imageView 和 Label的內(nèi)容 NSString *imageViewStr = [NSString stringWithString:_imageViewArr[indexPath.row]]; NSString *labelStr = [NSString stringWithString:_labelArr[indexPath.row]]; //將當(dāng)前單元格的內(nèi)容插入可變數(shù)組,作為第一個(gè)元素 [_imageViewArr insertObject:imageViewStr atIndex:0]; [_labelArr insertObject:labelStr atIndex:0]; //同時(shí)刪除可變數(shù)組中當(dāng)前單元格的原本所在位置 [_imageViewArr removeObjectAtIndex:indexPath.row + 1]; [_labelArr removeObjectAtIndex:indexPath.row + 1]; //更新tableView [_tableView reloadData]; //調(diào)用press方法, 變更tableView的高度 和 button的image [self press]; }- (void)press { //通過(guò)判斷select的值, 判斷單元格的展開(kāi)與收縮,更改tableView的高度 和 button的image if (_select == 0) { _select = 1; _tableView.frame = CGRectMake(100, 100, 130, 200); //以下使用masonry對(duì)tableView進(jìn)行更新約束 (以下代碼為更新tableView的高度)// [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {// make.height.mas_offset(200);// }]; [_button setImage:[UIImage imageNamed:@'kai'] forState:UIControlStateNormal]; } else { _select = 0; _tableView.frame = CGRectMake(100, 100, 130, 35);// [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {// make.height.mas_offset(self.view.frame.size.height * 0.0485);// }]; [_button setImage:[UIImage imageNamed:@'shou'] forState:UIControlStateNormal]; } [_tableView reloadData];}@end

// ViewTableViewCell.h 中#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface ViewTableViewCell : UITableViewCell@property UIImageView *iimageView;@property UILabel *label;@end

//ViewTableViewCell.m中#import 'ViewTableViewCell.h'@implementation ViewTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; _iimageView = [[UIImageView alloc] init]; [self.contentView addSubview:_iimageView]; _label = [[UILabel alloc] init]; [self.contentView addSubview:_label]; return self;}- (void)layoutSubviews { [super layoutSubviews]; _iimageView.frame = CGRectMake(5, 5, 25, 25); _label.frame = CGRectMake(37, 5, 80, 25); _label.font = [UIFont systemFontOfSize:15];}@end

效果圖如下

初始狀態(tài)

iOS實(shí)現(xiàn)折疊單元格

點(diǎn)擊cell或點(diǎn)擊按鈕,顯示如下:

iOS實(shí)現(xiàn)折疊單元格

點(diǎn)擊任意cell, 例如點(diǎn)擊掃一掃,單元格收回,如圖

iOS實(shí)現(xiàn)折疊單元格

再次展開(kāi)單元格, cell的內(nèi)容如下:

iOS實(shí)現(xiàn)折疊單元格

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 久久九九色 | 大视频在线爱爱爱爱 | 日本一级在线播放线观看视频 | 成人亚洲在线观看 | 日韩在线免费视频 | 高跟丝袜美女一级毛片 | 国产欧美精品一区二区三区四区 | 成人在线网| 欧美日韩精品一区二区三区视频 | 亚洲精品成人网久久久久久 | 成a人片亚洲日本久久 | 国产第三区 | 日本一区二区三区四区五区 | 鸥美毛片 | 亚洲一区二区在线免费观看 | 久草视频在线免费看 | 午夜性福 | 久艹视频在线免费观看 | 欧美激情一区二区三区高清视频 | 久久手机精品视频 | 一级风流片a级国产 | 久久亚洲精品无码观看不卡 | 免费一级毛片视频 | 成人性毛片 | 手机看片神马午夜片 | 欧美色网在线 | 免费在线观看一区 | 国产成人在线免费观看 | 亚洲欧美激情在线 | 成年人黄页 | 偷自拍第一页 | 国产欧美成人免费观看视频 | 免费看成人毛片 | 美女和男人免费网站视频 | 91精品啪在线看国产网站 | 手机看片日韩日韩韩 | 91看片淫黄大片.在线天堂 | 国产真实女人一级毛片 | 国产免费午夜a无码v视频 | 国产一级一级 | 国产在线a不卡免费视频 |