If we click on the Custom Section Header in a UITableView, move this section up

I have a Custom Section Header in a UITableView , in this section the title is placed by the UIButton on its very right. What I want, if I click on UIButton , then this Section Header should scroll up, What it

Any suggestion, part of the code will be admired.

+7
source share
2 answers

Step 1: Set the size of the section title. An example is as follows.

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 55; } 

Step 2: create and return the title of the custom section.

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)]; UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(0, 0, 320, 55)]; [btn setTag:section+1]; [aView addSubview:btn]; [btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown]; return aView; } 

Step 3: returns the number of partitions. (e.g. here 10)

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10; } 

Step 4: the number of rows in the section. (e.g. 4 rows for each section)

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; } 

Step 5: create and return a cell (UITableViewCell for each row)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } cell.textLabel.text=[NSString stringWithFormat:@"%i_%i",indexPath.section,indexPath.row]; return cell; } 

Step 6: Add an event to handle the TouchDown header.

 - (void)sectionTapped:(UIButton*)btn { [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } 
+17
source

You can use scrollToRowAtIndexPath: atScrollPosition: an animated method from a UITableView. Set the button tag in the section and call its action:

 [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
+2
source

All Articles