是否有可能有一个固定的uitableview标题使用部分?

这个问题不应该混淆在这里。 。 这是两件不同的事情。

有一个很好的例子,如何在SO上使用UITableView头。

这一切都工作正常,主标题固定在顶部,只要风格设置为plain

但是,如果我使用sections ,主标题不再坚持顶部,并移动到底部滚动。

在这种方法中,我将返回每个部分的标题。

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

在这个方法中,我将上面的标题部分的高度设置为:

 - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

在这个方法中,我设置了真正的表头。

 - (void)viewWillAppear:(BOOL)animated { ... self.recordTableView.tableHeaderView = headerView; } 

甚至有可能有一个固定的表头,而使用部分? 什么是这个替代解决scheme?

没有办法维护一个tableView的头部固定 ,但是当你需要一个唯一的头部时,一个有用的方法可以使用UIViewController而不是UITableViewController ,并设置标题( UIView )从tableView

像这样的东西:

在这里输入图像说明

如果你想要一个UITableViewController(静态单元/键盘处理),并有一个固定的头,那么你应该使用Containment。 你可以通过设置一个UIViewController与你的固定头,然后使用容器视图来embeddedUITableViewController从故事板。

Container View的Storyboard对象

一旦你有你的包含视图设置,你右键单击从容器视图拖动到您要embedded的视图控制器 – 在这种情况下的UITableViewController。

在这里输入图像说明

通过实现prepareForSegue:sender:方法,您可以从Container View控制器访问并获取对所包含的View Controller(UITableViewController)的引用。

这是Swift 3 。 我正在使用UITableViewController.

我已经声明类内variables:

 class ListController: UITableViewController { var headerView = UIView() // declared this as UIView just for reminder override func viewDidLoad() { ... } } 

然后我复制了我创build的constraints视图,这是在viewDidLoad

 class ListController: UITableViewController { var headerView = UIView() // declared this as UIView just for reminder override func viewDidLoad() { ... self.headerView = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self.tableView.tableHeaderView!)) as! UIView // optional self.headerView.backgroundColor = UIColor.white self.headerView.layer.shadowColor = UIColor.lightGray.cgColor self.headerView.layer.shadowOpacity = 0.1 self.headerView.layer.shadowRadius = 3 self.headerView.layer.shadowOffset = CGSize(width: 0, height: self.headerView.layer.shadowRadius*2) // this is important // you need to reassign selectors to the buttons // for views in self.headerView.subviews { let button = (views as? UIButton) if button != nil { if (button?.titleLabel?.text == self.btHome.titleLabel?.text) { button?.addTarget(self, action: #selector(btHomeAction), for: .touchUpInside) } else if (button?.titleLabel?.text == self.btEdit.titleLabel?.text) { button?.addTarget(self, action: #selector(btEditAction), for: .touchUpInside) } continue } } self.tableView.tableHeaderView = nil // removes the exising header created in storyboard self.tableView.tableFooterView = UIView() } } 

最后,在delegates

 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { return self.headerView } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return self.headerView.frame.height } 

这里是示例输出。

晚了,但我希望这会帮助别人,欢呼! 🙂

我没有做到这一点,但我想要尝试的第一件事是将我的tableview放在一个UIView并在那个UIView创build我自己的标题。 似乎一个微不足道的问题,使这个看法似乎是表头,它肯定会保持。

如果你想保持类作为一个UITableViewController你可以添加你的标题作为子视图到tableview的超级视图。 你还必须将tableview顶部向下插入,这样你的headerview不会隐藏表格。

这里是一个示例代码放在你的tableViewController子类(这个例子假设你的tableview控制器是在一个导航控制器,所以它推动视图下面的导航栏):

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.automaticallyAdjustsScrollViewInsets = NO; } -(void)addHeaderView{ CGFloat yPosition = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height; mainHeaderView = [[UIView alloc] init]; const CGFloat mainHeaderHeight = 44; [mainHeaderView setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, mainHeaderHeight)]; mainHeaderView.backgroundColor = [UIColor redColor]; [self.tableView.superview addSubview:mainHeaderView]; [self.tableView setContentInset:UIEdgeInsetsMake(yPosition + mainHeaderHeight, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)]; }