0

我正在使用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];
4

1 回答 1

5

与为您的实现子类化不同UITableViewController,您可能会发现将一个普通的子类化UIViewController并添加一个更容易UITableView。这将使您在自定义布局方面具有更大的灵活性。

基本上步骤是:

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

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

于 2012-09-24T19:55:51.993 回答