是一个UITableView可resize? 我可以分配一个不同的框架?

UITableView可resize? 我可以分配一个不同的框架?

我在UITableViewController中尝试这一行,但它不起作用(在iPhone上):

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

谢谢

不在UITableViewController中,因为在UITableViewController中, self.view == self.tableView

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

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

您可以使用以下设置顶部边距:

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

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

不应该使用UITableViewController,只需使用UIViewController和编程创buildUITableview

是的,UITableView是可resize的,但不是在使用UITableViewController时。

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

阅读以下两个链接

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

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

为了扩大Berk的答案:

  1. 使用XCode的模板创build一个常规的视图控制器。
  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]; } 

结果是具有可resize的tableView的UITableViewController。

你可以在类似的方法中调整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]]; } 

为了在使用UITableViewController时调整UITableView的大小,只需重写viewWillLayoutSubviews,如下所示:

 - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; CGRect frame = self.view.superview.bounds; frame.size.width -= 54.0; self.tableView.frame = frame; }