uitableview 中的节标题 - reusidentifier?

发布于 2024-10-01 13:56:13 字数 2258 浏览 6 评论 0原文

我有一个表格,其中包含各部分的自定义标题。除此之外,我还实现了折叠功能,以便当触摸标题时,所有非寻址部分都会缩小,而寻址部分则会展开。 我通过使用 UIButtons 作为背景视图意识到了这一点。此外,按钮具有不同的颜色和文本,具体取决于其状态(展开与未展开)。

我有一个问题,类似于重用标识符问题,即如果您不重用表中已分配的单元格,则如果您开始滚动,则会出现某些片段。在这里,它只发生在我的第一部分,尽管如此,它看起来支离破碎和重复......

是否有类似重用已经分配一次的 headerview 的东西,比如 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];
}

I have a table with customized headers for the sections. In addition to that, I implemented a collapsing feature, so that when a header is touched, all non-adressed sections are reduced, and the adressed section is expanded.
I realized that by using UIButtons as background views. Furthermore, the buttons have different colors and text, depending on their state (expanded vs not expanded).

I have a problem, similar to that reusidentifier problem, i.e. if u dont reuse cells already allocated in a table, certain fragments appear, if u start to scroll. Here it only happens to my first section, nevertheless it appears fragmented and duplicated....

Is there anything similar to reusing headerviews already allocated once like the method reusIdentifier for UITableViewCells?

Or is there a better approach?

Allocating the Buttons in advance to store them in array didnt do the trick for me...

Here's some code:

- (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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-10-08 13:56:13

我还使用 UIButton 标题视图实现了折叠节表。我发现尝试缓存和重用视图是愚蠢的,因为你不知道 UITableView 何时完成它们(我崩溃了)。

我建议通过以下方式实现状态更改: 更新数据模型;执行所有行插入/删除以执行折叠/展开;然后执行 [tableView reloadData],它将调用 viewForHeaderInSection,其中您使用数据模型的状态返回适当格式的 uibutton 视图。

I also implemented a collapsing section table using UIButton header views. What I found was that it was folly trying to cache and resuse the views, because you don't know when the UITableView is done with them (I got crashes).

I recommend implementing your state change by: updating your data model; performing all the row insert/deletes to perform the collapses/expand; then doing a [tableView reloadData], which will call viewForHeaderInSection, where you use the state of your data model to return the appropriately formatted uibutton view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文