![](/img/trans.png)
[英]How to collapse the section when user click on the header of the section in UITableView?
[英]if we click on Custom Section Header in UITableView, then move that section to top
我在UITableView
有一个Custom Section Header
,在那个部分标题上放置了UIButton
就在它的右边。我想要的是,如果我点击UIButton
,那个特定的Section Header
应该滚动到Top。 而已
任何建议,一段代码都会受到赞赏。
第1步:设置Section标头的大小。 示例如下。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 55;
}
第2步:创建并返回自定义节标题。
- (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)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
第4步:每个部分的行数。 (例如,每个部分4行)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
第5步:创建并返回单元格(每行的UITableViewCell)
- (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 。
- (void)sectionTapped:(UIButton*)btn {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
您可以使用UITableView中的scrollToRowAtIndexPath:atScrollPosition:animated:方法。 将按钮标记设置为该部分并调用其操作:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag]
atScrollPosition:UITableViewScrollPositionTop animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.