iOS 개발 중 TableView 와 유사 한 QQ 패 킷 접 기 및 펼 치기 효과
그래서 생각 을 바 꿔 서 table View 의 HeaderView 를 사 용 했 습 니 다.헤 더 뷰 에 손짓 을 더 해 접 고 펼 치 는 문 제 를 쉽게 해결 합 니 다.
그냥 코드 올 려.
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *listArray; //
@property (nonatomic, strong) NSMutableArray *titlesArray; //
@property (nonatomic, strong) NSMutableDictionary *openSectionDict; //
- (void)viewDidLoad {
[super viewDidLoad];
// tableView
_myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.openSectionDict = [[NSMutableDictionary alloc] init]; //
[self setUpData];
}
//
- (void)setUpData {
self.listArray = [NSMutableArray new];
self.titlesArray = [NSMutableArray new];
for (int i = 0; i < 5; i++) { // 5 section
[self.titlesArray addObject:[NSString stringWithFormat:@"section %d", i]];
NSMutableArray *array = [NSMutableArray new];
for (int i = 0; i < 4; i++) { // section 4 row
[array addObject:[NSString stringWithFormat:@"row %d", i]];
}
[self.listArray addObject:array];
}
}
// tableView
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) { // row
return 0;
} else {
return 4;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL_ID"];
cell.textLabel.text = [NSString stringWithFormat:@"row %ld", indexPath.row];
}
return cell;
}
#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
view.backgroundColor = [UIColor whiteColor];
view.tag = KTAG + section;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, view.bounds.size.width, view.bounds.size.height)];
label.text = self.titlesArray[section];
[view addSubview:label];
if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 10) / 2, 7, 10)];
imageView.image = [UIImage imageNamed:@"Triangle_right_gray"]; //
[view addSubview:imageView];
} else {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 7) / 2, 10, 7)];
imageView.image = [UIImage imageNamed:@"Triangle_down_gray"];
[view addSubview:imageView];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collegeTaped:)];
[view addGestureRecognizer:tap];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.1;
}
#pragma mark - sectionHeader clicked
- (void)collegeTaped:(UITapGestureRecognizer *)sender {
NSString *key = [NSString stringWithFormat:@"%ld", sender.view.tag - KTAG];
//
if ([[self.openSectionDict objectForKey:key] integerValue] == 0) {
[self.openSectionDict setObject:@"1" forKey:key];
} else {
[self.openSectionDict setObject:@"0" forKey:key];
}
NSUInteger index = sender.view.tag;
NSIndexSet *set = [NSIndexSet indexSetWithIndex:index - KTAG];
[self.myTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}
마지막 효과:위 에서 설명 한 것 은 편집장 님 께 서 소개 해 주신 iOS 개발 에서 TableView 는 QQ 와 같은 그룹의 접 기와 전개 효과 입 니 다.여러분 께 도움 이 되 셨 으 면 좋 겠 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
TIL - 2021.05.13 (Thu)야곰 캠프 방학 중 TIL Notion -> Velog 이전하면서 복습하기! 일단 뷰의 재사용을 위해서 재사용되는 cell에 남아있는 text, image 등을 털어내야 한다 이미 구현되어 있는 prepareForR...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.