iOS 개발 중 TableView 와 유사 한 QQ 패 킷 접 기 및 펼 치기 효과

QQ 그룹 과 같은 모양 으로 tableView 의 접 기와 펼 침 을 실현 합 니 다.사실 이 효 과 를 하려 면 제 가 먼저 생각 한 것 은 tableView 에 여러 개의 tableView 를 끼 워 넣 는 것 입 니 다.이 생각 은 실현 하기 가 좀 어렵 습 니 다.
그래서 생각 을 바 꿔 서 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 와 같은 그룹의 접 기와 전개 효과 입 니 다.여러분 께 도움 이 되 셨 으 면 좋 겠 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기