2014-02-26 95 views
8

請幫我提出這個問題。我嘗試了所有可以在這裏找到的選擇。動態更改UITableView高度iOS7

我的代碼示例附加(我知道,這是非常糟糕的)。
如何動態更改UITableView高度?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self vozvratmassiva]; 
} 

- (NSMutableArray *) vozvratmassiva 
{ 
... 
    return array; 
} 

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSArray *a = [self vozvratmassiva]; 
    return a.count; 
} 

-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    .... 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    .... 
    return commonsize; 
} 

@end 
+0

你想改變的tableView的高度或細胞的高度動態。 – Sandeep

+0

我想改變tableView的常見大小。我確實已經改變了細胞的高度。 – serg1991

+0

我認爲這個答案應該更符合您的要求 http://stackoverflow.com/a/18784825/457998 –

回答

-3

更改單元格高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return commonsize; 
} 

要改變的tableView高度:

tableView.size.height = 100; 
+0

我該如何總結這個tableview中所有單元格的高度? – serg1991

+0

cellheight *細胞數目? – xoail

+0

不,我的細胞有不同的高度 – serg1991

3

我明白你想要做什麼。這裏是你應該做的:

  1. 添加高度約束到您的UITableView
  2. 包裝在自定義的UIView
  3. 創建自定義類MyCustomView:UIView的IB中
  4. Set類爲您wraper的UIView到您的從步驟3類
  5. 從約束在IB到類
  6. 進行連接使表視圖之間以及類
  7. 認沽共同的連接去到你的新類:
- (void) layoutSubviews { 
    // set height 0, will calculate it later 
    self.constraint.constant = 0; 

    // force a table to redraw 
    [self.tableView setNeedsLayout]; 
    [self.tableView layoutIfNeeded]; 

    // now table has real height 
    self.constraint.constant = self.tableView.contentSize.height; 
} 
+0

你不覺得你應該添加這個答案作爲評論? –

+0

Kirill的答案可能有助於此鏈接中的某人 - > http://stackoverflow.com/questions/18527227/uitableviewcell-with-dynamic-height-ios –