日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

仿QQ联系人的TableView的折叠与拉伸

發(fā)布時(shí)間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 仿QQ联系人的TableView的折叠与拉伸 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近手上任務(wù)比較輕,打算把一些之前做一些的簡單的東西再整理整理,優(yōu)化一下,做個(gè)記錄;?

TableView的折疊拉伸主要是在一些聯(lián)系人分組里會(huì)經(jīng)常見到,今天做了一個(gè)簡單的demo,代碼也做了處理,隨拿隨用。

思路整理:

1.根據(jù)數(shù)據(jù)設(shè)置分組

2.設(shè)置折疊/拉伸狀態(tài)標(biāo)識(shí)

3.實(shí)現(xiàn)分組透視圖點(diǎn)擊事件

實(shí)現(xiàn)代碼:

#import "FoldTableViewController.h"

#define STATE @"state"

#define INFO @"info"

@interface FoldTableViewController ()

{

NSMutableArray *_dataArray;//數(shù)據(jù)源數(shù)組

}

@end

@implementation FoldTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self setExtraCellLineHidden];

[self requestData];

}

//底部視圖留白

- (void)setExtraCellLineHidden{

UIView *view = [UIView new];

view.backgroundColor = [UIColor clearColor];

[self.tableView setTableFooterView:view];

}

//創(chuàng)建數(shù)據(jù)

- (void)requestData{

/**

假設(shè)有i組數(shù)據(jù),每組有j條內(nèi)容:

每組實(shí)例化一個(gè)可變字典存儲(chǔ)組內(nèi)數(shù)據(jù),STATE對(duì)應(yīng)當(dāng)前狀態(tài)(折疊/拉伸),INFO對(duì)應(yīng)當(dāng)前內(nèi)容;

*/

_dataArray = [[NSMutableArray alloc]init];

for (int i = 0; i < 5; i++) {

NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];

NSMutableArray *tempArr = [[NSMutableArray alloc]init];

for (int j = 0; j < 10; j++) {

NSString *infoStr = [NSString stringWithFormat:@"test%d",j];

[tempArr addObject:infoStr];

}

[tempDict setValue:@"1" forKey:STATE];

[tempDict setValue:tempArr forKey:INFO];

[_dataArray addObject:tempDict];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return _dataArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSString *state = _dataArray[section][STATE];

if ([state isEqualToString:@"0"]) {

return 0;

}

return [_dataArray[section][@"info"] count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *ide = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ide];

}

//取到當(dāng)前cell的內(nèi)容

cell.textLabel.text = _dataArray[indexPath.section][@"info"][indexPath.row];

return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)];

headView.tag = 10+section;

headView.backgroundColor = [UIColor grayColor];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];

[headView addSubview:imageView];

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 5, 100, 20)];

titleLabel.text = [NSString stringWithFormat:@"section%ld",section];

titleLabel.textColor = [UIColor whiteColor];

[headView addSubview:titleLabel];

/**在刷新視圖時(shí),根據(jù)當(dāng)前分組的狀態(tài),調(diào)整頭視圖的內(nèi)容視圖

*/

NSDictionary *dict = _dataArray[section];

if ([dict[STATE] isEqualToString:@"1"]) {

imageView.image = [UIImage imageNamed:@"arrow_spread"];

}else{

imageView.image = [UIImage imageNamed:@"arrow_fold"];

}

/**為頭視圖添加輕觸事件

*/

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headViewClick:)];

[headView addGestureRecognizer:tap];

return headView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 30;

}

- (void)headViewClick:(UITapGestureRecognizer *)tap{

NSInteger index = tap.view.tag-10;

NSMutableDictionary *dict = _dataArray[index];

/**點(diǎn)擊頭視圖,改變?cè)摻M狀態(tài)

*/

if ([dict[STATE] isEqualToString:@"1"]) {

[dict setValue:@"0" forKey:STATE];

}else{

[dict setValue:@"1" forKey:STATE];

}

/**刷新當(dāng)前點(diǎn)擊分組的數(shù)據(jù)

reloadSections:需要刷新的分組

withRowAnimation:刷新的動(dòng)畫方式

*/

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

轉(zhuǎn)載于:https://juejin.im/post/5a31d4d451882526151a9ac2

總結(jié)

以上是生活随笔為你收集整理的仿QQ联系人的TableView的折叠与拉伸的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。