UITableView粘页脚/自定义工具栏实现

我正在使用UITableViewController ,我想要做的是在UIView下添加一个自定义工具栏。

我已经尝试启用navigationController (下面的代码)的工具栏,但它似乎不会正常工作。 UITextField不会调用委托,文本字段的按键不显示在文本字段中。

使用这样的工具栏不是我的第一select,我想我的自定义视图下我的UITableViewController我可以把我的项目,这就像一个UIToolbar。 (保持为UIView页脚)

码:

  self.navigationController.toolbarHidden = NO; // create toolbar objects UITextField *inputField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 230, 31)]; inputField.backgroundColor = [UIColor clearColor]; inputField.borderStyle = UITextBorderStyleRoundedRect; inputField.inputAccessoryView = self.navigationController.toolbar; inputField.returnKeyType = UIReturnKeyDone; inputField.delegate = self; UIButton *sendButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; sendButton.titleLabel.text = @"Send"; sendButton.backgroundColor = [UIColor greenColor]; // add objects into navigation controller self.toolbarItems = [NSArray arrayWithObjects: [[UIBarButtonItem alloc] initWithCustomView:inputField], [[UIBarButtonItem alloc] initWithCustomView:sendButton], nil]; 

而不是为你的实现UITableViewController ,你可能会发现更简单的UIViewController子类并添加一个UITableView 。 这将使您在定制布局方面有更大的灵活性。

基本上这些步骤是:

  1. 让你的类inheritanceUIViewController(而不是UITableViewController)
  2. 让你的类实现UITableViewDelegate和UITableViewDataSource协议
  3. 在你的类中实现UITableViewDelegate和UITableViewDataSource方法(例如cellForRowAtIndexPath :, didSelectRowAtIndexPath:等)
  4. 在IB中,将UITableView作为子视图添加到视图控制器的视图中
  5. 设置刚刚添加的视图控制器类的表视图的委托和数据源属性
  6. 调整UITableView的框架大小,以便在底部为您的inputField和button留出空间

这样,你可以添加inputField和button到viewController的视图(在底部),他们不会滚动,因为他们是从tableview分开。