2

我有一个奇怪的问题。

我正在继承 UIViewController 并添加一个 tableView 属性,我在其中加载一个 UITableView。现在我将此 UITableView 添加到父级的子视图中。工作正常,但我得到两个TableViews。一种标准样式的 TableView 和一种我希望它位于标准样式之上的方式。两者都包含正确的数据。

@interface RootViewController : UIViewController <ABPersonViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
  UITableView *tableView;
  ToolbarController *toolbar;
  ...
}

@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) ToolbarController *toolbar;
...

@end

@implementation RootViewController
- (void)viewDidLoad {
  [super viewDidLoad];

  CGRect mainViewBounds = self.parentViewController.view.bounds;
  CGFloat toolbarHeight = 44;

  toolbar = [[ToolbarController alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(mainViewBounds), toolbarHeight) parentView:self]; 

  tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight) style:UITableViewStylePlain];
  tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  tableView.rowHeight = 60.0;
  [tableView tableView].delegate = self;
  [tableView tableView].dataSource = self;
  tableView.backgroundColor = [UIColor clearColor];

  [self.parentViewController.view addSubview:tableView];
  [self.parentViewController.view addSubview:toolbar];
}
@end

- - - 更新 - - -

我只是错误地粘贴了一部分

[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;

实际上是在代码中

tableView.delegate = self;
tableView.dataSource = self;

--------更新 2--------

如果我不创建自己的 UITableView

tableView = [[UITableView alloc] ...]

然后有一个自动为我设置。这对我来说很好......我不需要初始化一个,但我不能改变标准的框架。

tableView.frame = CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight);

设置框架没有任何影响......

4

3 回答 3

1

我遇到了同样的问题,您可能正在将 UITableViewController 转换为 UIViewController,而您的 RootViewController 可能具有 uitableview。

使用 IBOutlet 在 RootviewController 中创建 _tableview 的属性。并将其与您的 XIB 中的 tableview 链接。

不要在 RootviewController 中分配 _tableview 并且不要 addsubview it 。

于 2012-05-24T06:08:06.783 回答
0

取而代之的是,

[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;

您是否尝试过仅使用:

tableView.delegate = self;
tableView.dataSource = self;

我不知道为什么会这样,但这是唯一突出的东西。还要确保你没有在 IB 中设置另一个 UITableView。

它也可能与工具栏控制器有关。我假设它是一个自定义类,所以请确保您没有在其中创建 UITableView。

于 2010-03-18T16:44:18.417 回答
0

我遇到了同样的问题...

不要直接将tableview添加到视图控制器([self.parentViewController.view addSubview:tableView];),而是使用以下方式..

UIView *totalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[totalView addSubview:tableView];
self.view=totalView;

希望这能解决它(也适用于@kris!)

于 2011-12-28T12:19:58.487 回答