2011-08-23 176 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; 
} 
相關問題