I have a Custom Section Header
in UITableView
, on that section header there placed UIButton
on its very right.What i want is, if i click o
Step 1: set the size of Section header. Example as follows.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 55;
}
Step 2: create & return the customized section header.
- (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: return number of sections. ( for example 10 here )
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
Step 4 : number of rows per section. ( for example, 4 rows for each section )
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
Step 5 : create & return 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 the event to handle the TouchDown on section header.
- (void)sectionTapped:(UIButton*)btn {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
You can use the scrollToRowAtIndexPath:atScrollPosition:animated: method from UITableView. Set the button tag to the section and call in his action:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag]
atScrollPosition:UITableViewScrollPositionTop animated:YES];