是一个UITableView可调整大小?我可以分配不同的框架吗?

问题描述:

UITableView是否可调整大小?我可以分配不同的框架吗?是一个UITableView可调整大小?我可以分配不同的框架吗?

我想在一个UITableViewController这条线,但它并没有(对iPhone)工作:

self.tableView.frame = CGRectMake(0, 0, 300, 200); 

感谢

不在一个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; } –

是的,UITableView是可调整大小的,但不是当您使用UITableViewController时。

尝试添加一个UITableView到一个普通的UIViewController并让它实现所需的委托方法。这样你可以更灵活地定制你的UITableView。

阅读以下两个链接

http://www.skylarcantu.com/blog/2009/09/24/dont-use-uitableviewcontroller-really/

http://flux88.com/2010/03/is-uitableviewcontroller-useless/

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

  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

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

您可以通过设置上边距:

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

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

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

+1

你太棒了! – jxdwinter

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

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

它的工作原理也viewDidAppear ...

尝试使用此:

- (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。 –

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

- (void)viewWillLayoutSubviews { 
    [super viewWillLayoutSubviews]; 

    CGRect frame = self.view.superview.bounds; 
    frame.size.width -= 54.0; 
    self.tableView.frame = frame; 
}