在故事板中创建自定义UITableview节头的最佳方法 [英] Best way to create custom UITableview section header in storyboard

查看:128
本文介绍了在故事板中创建自定义UITableview节头的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我在故事板中创建原型单元格并将此单元格用作节标题。
在tableView:viewForHeaderInSection:方法中,我将单元格出列并返回它。

Current I am creating a prototype cell in storyboard and using this cell as a section header. Inside tableView:viewForHeaderInSection: method, I am dequeuing the cell and returning it.

我的节标题单元格中有一个UITextField和一个UIButton。
当我点击文本字段键盘时,但是一旦焦点从文本字段移开,整个部分标题就会消失。
当我直接将单元格作为节标题视图返回时会发生这种情况,但是如果我将新分配的UIView作为节标题视图返回到哪个单元格被添加为子视图,那么除了自动调整掩码外,一切正常。

My section header cell has a UITextField and a UIButton in it. When I tap on text field keyboard appears but as soon as focus is moved away from text field whole section header disappears. This happens when I return the cell directly as section header view, but if I return a newly allocated UIView as section header view onto which cell is added as subview then everything works fine besides autoresizing masks.

为什么标题消失了?

我不确定这里最好的东西是什么。

I am not sure what could be the best thing todo here.

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *CellIdentifier = @"SectionHeader"; 

SettingsTableViewCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //return sectionHeaderCell; // returning cell directly, section header disappears when focus is moved away from text field.
UIView * headerView = [[UIView alloc] initWithFrame:sectionHeaderCell.frame];
[headerView addSubView:sectionHeaderCell];
return sectionHeaderCell;//header view never disappears, but auto resizing masks do not work. Need to know how to set autoresizing masks to headerView so that it resizes correctly.

}  


推荐答案

原型单元格表视图只允许您在情节提要编辑器中设计单元格,而不是节页眉和页脚。您尝试使用 UITableViewCell 作为节标题是一个聪明的黑客,但它只是不支持所涉及的类 - UITableViewCell 不适用于除表格单元格以外的任何其他内容。它可能比视图消失或没有正确布局更糟糕; UIKit将完全有权使主张失败,删除所有应用程序的数据,撤销您的开发人员证书,或者让您的房子着火。

Prototype cell table views only allow you to design cells in the storyboard editor, not section headers and footers. Your attempt to use a UITableViewCell as the section header is a clever hack, but it's just not supported by the classes involved—UITableViewCell is not designed to be used for anything other than a table view cell. It could do a lot worse than the view disappearing or not being laid out correctly; UIKit would be well within its rights to fail an assertion, delete all the app's data, revoke your developer certificate, or set your house on fire.

如果您需要代码要正常运行,您可以选择在代码中创建节标题或将它们放在单独的XIB文件中。我知道那不是你想做的,但这些都是你的选择。

If you want your code to function properly, your choices are to either create your section headers in code or to put them in a separate XIB file. I know that's not what you want to do, but those are the options you have.

这篇关于在故事板中创建自定义UITableview节头的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆