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

Bar*_*rtu 0 iphone delegates uitableview ios

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

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

使用像这样的工具栏不是我的第一选择,我想在我的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];
Run Code Online (Sandbox Code Playgroud)

jon*_*oll 5

UITableViewController您可能会发现将子类化为子类并向其UIViewController添加a 更容易,而不是为您的实现创建子类UITableView.这样可以在自定义布局方面提供更大的灵活性.

基本上步骤是:

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

这样你就可以将inputField和button添加到viewController的视图中(在底部),它们不会滚动,因为它们与tableview是分开的.