UITableView粘贴页脚/自定义工具栏实现 [英] UITableView sticky footer / custom toolbar implementation

查看:105
本文介绍了UITableView粘贴页脚/自定义工具栏实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITableViewController ,我想做的是在 UIView 。

I am using a UITableViewController, and what I would like to do is to add a custom toolbar under the UIView.

我尝试启用 navigationController (代码如下)的工具栏,但似乎不会好好工作。 UITextField 不会调用代理,文本字段的按键不会显示在textfield本身中。

I have tried enabling the toolbar of the navigationController (code below) but it won't seem to work properly. UITextField won't call delegates, and key presses of the text field are not shown in the textfield itself.

使用这样的工具栏不是我的第一选择,我想在我的UITableViewController下面有我的自定义视图,我可以把我的项目,像UIToolbar一样。 (保留为UIView页脚)

Using a toolbar like this is not my first choice, I would like to have my custom view under my UITableViewController where I can put my items, which acts like a UIToolbar. (stays as a UIView footer)

代码

    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 给它这将为您在自定义布局方面提供更多的灵活性。

Instead of subclassing UITableViewController for your implementation, you might find it easier to subclass a plain UIViewController and add a UITableView to it. This would give you more flexibility in terms of customizing the layout.

基本上这些步骤将是:


  1. 让你的类继承自UIViewController(而不是UITableViewController)

  2. 让你的类
    实现UITableViewDelegate和UITableViewDataSource
    协议

  3. 在您的类中实现UITableViewDelegate和
    UITableViewDataSource方法(例如cellForRowAtIndexPath :, didSelectRowAtIndexPath :, etc)

  4. 在IB中添加
    UITableView作为您的视图控制器视图的子视图

  5. 设置您刚添加
    的表视图的
    委托和数据源属性作为您的viewcontroller类

  6. 调整
    UITableView的框架大小,以允许您的inputField和按钮底部的空格

  1. Have your class inherit from UIViewController (instead of UITableViewController)
  2. Have your class implement the UITableViewDelegate and UITableViewDataSource protocols
  3. Implement the UITableViewDelegate and UITableViewDataSource methods in your class (e.g. cellForRowAtIndexPath:, didSelectRowAtIndexPath:, etc)
  4. In IB, add a UITableView as a subview to your view controller's view
  5. Set the delegate and datasource properties of the table view you just added to be your viewcontroller class
  6. Resize the frame of the UITableView to allow space at the bottom for your inputField and button

这样,您可以将inputField和按钮添加到viewController的视图(位于底部),并且它们将不会滚动,因为y与表格分开。

This way you can add the inputField and button to your viewController's view (at the bottom) and they won't scroll because they are separate from the tableview.

这篇关于UITableView粘贴页脚/自定义工具栏实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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