我有一个带有自定义标题的表格。除此之外,我实现了一个折叠功能,这样当一个标题被触摸时,所有非地址部分都被缩小,而地址部分被展开。通过使用 UIButtons 作为背景视图,我意识到了这一点。此外,按钮具有不同的颜色和文本,具体取决于它们的状态(展开与未展开)。
我有一个问题,类似于那个 reusidentifier 问题,即如果你不重用已经在表格中分配的单元格,如果你开始滚动,就会出现某些片段。在这里它只发生在我的第一部分,但它似乎支离破碎和重复......
有什么类似于重用已经分配一次的标题视图,比如 UITableViewCells 的方法 reusIdentifier 吗?
还是有更好的方法?
提前分配按钮以将它们存储在数组中并没有为我解决问题......
这是一些代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
[button setShowsTouchWhenHighlighted:NO];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(collapse:) forControlEvents:UIControlEventTouchUpInside];
button.tag = section;
[button setAdjustsImageWhenHighlighted:NO];
button.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:154/255.0f blue:201/255.0f alpha:1.0f];
[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
if (section == 0) {
// [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[button setTitle:@" Newsroom" forState:UIControlStateNormal];
} else {
[button setTitle:[NSString stringWithFormat:@" Sektion %d", section] forState:UIControlStateNormal];
}
if (section == mySection) {
[button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_active.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_active.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor whiteColor]];
} else {
[button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_inactive.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"sectionHeader_inactive.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor colorWithRed:96.0f/255.0f green:96/255.0f blue:97/255.0f alpha:1.0f]];
}
return [button autorelease];
}