gpt4 book ai didi

ios - 如何重用单个 xib 作为 tableview 的节标题?

转载 作者:行者123 更新时间:2023-11-28 19:05:19 26 4
gpt4 key购买 nike

我想在我的 tableview 上利用 Apple 的锁定节标题控件(节标题粘在顶部,直到被下一个节标题向上推),但我想使用我自己的 View ,以及一个我是 xib 设计的。

在我的应用程序的其他地方,我在代码中为节标题创建了 View 并且一切都很好,但是当尝试使用 xib 为 View 做同样的事情时,它会出现问题。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
ProductSectionHeader* header = self.productHeaderView;
if (!header)
{
[[NSBundle mainBundle] loadNibNamed:@"ProductSectionHeader" owner:self options:nil];
}
header.nameLabel.text = @"Some person";
header.locationLabel.text = @"Some place";

return header;
}

这显示了第一部分中的标题 View (每个单元格都有自己的部分),但是当提示显示第二个标题时,所有标题都消失了。似乎只允许我引用 xib 或其他内容。

那么我如何重用 xib 在一个 Controller 中创建该 View 的多个实例,以及我如何将它应用到节标题中?

最佳答案

So how do I reuse a xib to create multiple instances of that view in one controller, and how do I employ that into section headers?

每次加载 .xib 时都会创建 View 的新副本。诀窍是将新加载的 View 保存在不同的属性或变量中,然后清除连接到 View 的 socket ,以便您可以加载另一个副本。所以,像这样,假设 productHeaderView 是连接到标题 View 的导出:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// load the view from the .xib
[[NSBundle mainBundle] loadNibNamed:@"ProductSectionHeader" owner:self options:nil];

// save it in a different variable
ProductSectionHeader* header = self.productHeaderView;

// clear the outlet for next time
self.productHeaderView = nil;

if (!header)
{
NSLog(@"There was a problem loading the view.")
}
header.nameLabel.text = @"Some person";
header.locationLabel.text = @"Some place";

return header;
}

我还应该指出相对较新的 UITableView 方法 -registerNib:forHeaderFooterViewReuseIdentifier: ,这让事情变得更容易。您可以只向表格注册一个 nib 对象,让表格负责加载 View 。表格单元格也有类似的方法。

关于ios - 如何重用单个 xib 作为 tableview 的节标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20958484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com