2011-08-23 188 views

回答

17

不在一个UITableViewController,因为在一个UITableViewController,self.view == self.tableView

尝试使用UIViewController,根据需要实现UITableViewDataSource和UITableViewDelegate协议。

+0

另外self.view.frame = CGRectMake(0,0,300, 200);不起作用。 – aneuryzm

+0

@帕特里克:正好。 –

+0

确定它是: - (void)viewWillLayoutSubviews {super viewWillLayoutSubviews]; CGRect frame = self.view.superview.bounds; frame.size.width - = 54.0; self.tableView.frame = frame; } –

4

为了扩大对伯克的回答是:

  1. 使用Xcode的模板创建常规视图控制器。
  2. 添加<UITableViewDelegate, UITableViewDataSource>
  3. 添加一个@property IBOutlet UITableView,将一个UITableView对象添加到XIB并链接它们。
  4. UITableView的代理和数据源设置为类。
  5. 添加以下方法:

// #编译马克 - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"ServiceCell"; 
    UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] init]; 
    } 
    return [cell autorelease]; 
} 

的结果是一个可调整大小的tableView一个UITableViewController。

+0

有没有办法将UITableViewController转换成UIViewCOntroller,或者我必须完全复制所有代码?!? (我没有使用nib文件) – aneuryzm

+0

你不能转换它,但是如果你使用或不使用NIB创建一个新的UIViewController,你可以从你的UITableViewController中复制粘贴所有的UITableViewDelegate和UITableViewDatasource方法。请注意,按照上述步骤,您将新控制器设置为内部tableView的委托和数据源,因此它调用相同的方法。没有NIB的区别在于你必须做tableView = [[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped] autorelease]; tableView.delegate =自我; tableView.dataSource =自我;在viewDidLoad方法中。 – Jano

+0

我刚刚转换它。添加几行。没有NIB很简单。 – aneuryzm

7

如果您使用的UITableViewController您不能调整self.tableview.frame或者self.view.frame但是你可以做的是

您可以通过设置上边距:

UIEdgeInsets inset = UIEdgeInsetsMake(50, 0, 0, 0); 
self.tableView.contentInset = inset; 

这不是一个好的做法,以防万一你想要更多的空间,你可以使用它。

不应该使用的UITableViewController,只需简单地使用的UIViewController和编程创建的UITableView

+1

你太棒了! – jxdwinter

0

,您可以调整的UITableViewController内的tableView的方法,比如

- (void)viewWillAppear:(BOOL)animated 
{ 
    [self.tableView setFrame:CGRectMake(x, y, w, h)]; 
} 

它的工作原理也viewDidAppear ...

0

尝试使用此:

- (void)viewDidAppear:(BOOL)animated 
{ 
    // Set your tableView frame in UITableViewController 
    [self.tableView setFrame:CGRectMake(0, 320, 320, 480)]; 
    // To remove the black color space up of the table view and in this case I set it as white 
    [[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]]; 
} 
+0

当我这样做时,用户可以看到发生的变化。 它看起来很jump。 –

0

为了调整大小的UITableView同时使用的UITableViewController只重写viewWillLayoutSubviews这样的:

- (void)viewWillLayoutSubviews { 
    [super viewWillLayoutSubviews]; 

    CGRect frame = self.view.superview.bounds; 
    frame.size.width -= 54.0; 
    self.tableView.frame = frame; 
} 
相关问题