如果我们点击UITableView中的Custom Section Header,那么将该部分移到顶部 [英] if we click on Custom Section Header in UITableView, then move that section to top
问题描述
我在 UITableView
中有一个自定义剖面标题
,在该部分标题上放置 UIButton
就在它的右边。我想要的是,如果我点击 UIButton
,那个特定的 Section Header
应滚动到顶部。这就是它
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 on UIButton
, that particular Section Header
should scrolls to Top. That's it
任何建议,一段代码都会受到赞赏。
Any suggestion, piece of code will be admired.
推荐答案
步骤1:设置Section标头的大小。示例如下。
Step 1: set the size of Section header. Example as follows.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 55;
}
第2步:创建&返回自定义节标题。
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;
}
第3步:返回部分数量。 (例如10)
Step 3: return number of sections. ( for example 10 here )
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
步骤4:每个部分的行数。 (例如,每个部分4行)
Step 4 : number of rows per section. ( for example, 4 rows for each section )
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
第5步:创建&返回单元格(每行的UITableViewCell)
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;
}
第6步:添加事件以处理部分标题上的 TouchDown 。
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];
}
这篇关于如果我们点击UITableView中的Custom Section Header,那么将该部分移到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!