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]; }
Sagar R. Kothari
source share