2015-05-29 53 views
0

我想要做这样的事情,我有UIViewControler。所以在我的故事板中,拖放一个UITableView到视图中。我应该在该表格视图中显示少量自定义行。但我想动态地改变该表视图的高度(根据表视图中的行数)。TableView高度不能在IOS中调整大小

我试过像下面的代码,但没有任何工作到目前为止。

CGRect tvframe = [tableView frame]; 
[tableView setFrame:CGRectMake(tvframe.origin.x, 
          tvframe.origin.y, 
          tvframe.size.width, 
          tvframe.size.height + 20)]; 

而且我已经试过这

CGRect frame = self.tableView.frame; 
    frame.size = self.tableView.contentSize; 
    self.tableView.frame = frame; 

但没有工作对我来说..请一些人告诉我怎么做我这样做。

谢谢。

+0

您在设置表格框架的哪个事件? –

+0

你在使用autolayout吗? –

+0

@SanjayMohnani我使用这个视图DadLoad事件 – Darshana

回答

1

在自动布局。

enter image description here

设置约束网点,并通过改变constant值:

self.sampleConstraint.constant = 20 
self.view.layoutIfNeeded() 
+0

我会试试这个,让你知道..谢谢 – Darshana

+0

好吧,你必须添加一个高度约束。并在数据填入表格时进行更改。 @Darshana –

+0

谢谢你......这对我来说完美无缺:) – Darshana

0
CGRect frame = self.tableView.frame; 
frame.size.height = resourceArray.count*row_height; 
self.tableView.frame = frame; 
0

,如果你想改变的框架,然后尝试的限制(的NSLayoutConstraintIBOutlet)只要使用这个

tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
0

尝试下面的代码,

CGFloat height = self.tableView.rowHeight; 
height *= [myArray count]; 

CGRect tableViewFrame = self.tableView.frame; 
tableViewFrame.size.height = height; 
self.tableView.frame = tableViewFrame; 
0

试试这个:

[UIView animateWithDuration:0.1 animations:^{ 
    CGRect frame = self.tableView.frame; 
    frame.size.height = 100.0; // expected height 
    self.tableView.frame = frame; 
}]; 
1

我希望这个答案可以帮助你...

For Dynamic TableviewCell Height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *OptName_length = @" Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam"; 
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0]}; 
CGRect rect = [OptName_length boundingRectWithSize:CGSizeMake(tableView.bounds.size.width-150.0, MAXFLOAT) 
               options:NSStringDrawingUsesLineFragmentOrigin 
              attributes:attributes 
               context:nil]; 

CGSize requiredSize = rect.size; 
return requiredSize.height +5.0f; 
} 

值150.0是单元格中标签或textview的静态宽度。或前导或尾随其他元素的标签宽度或水平空间值。将单元格中的textview的前导,尾部,顶部和底部约束设置为不设置静态高度

将tableview高度调整为no。细胞的...

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:YES]; 
[self adjustHeightOfTableview]; 
} 

- (void)adjustHeightOfTableview 
{ 
CGFloat height = tblMainMenu.contentSize.height; 
CGFloat maxHeight = tblMainMenu.superview.frame.size.height - tblMainMenu.frame.origin.y; 

// if the height of the content is greater than the maxHeight of 
// total space on the screen, limit the height to the size of the 
// superview. 

if (height > maxHeight) 
    height = maxHeight; 

// now set the height constraint accordingly 
self.tableViewHeightConstraint.constant = height; 
[UIView animateWithDuration:0.1 animations:^{ 
    [self.view setNeedsUpdateConstraints]; 
}]; 
} 

Tableview auto layout constraint image

0

我希望它有助于..

,如果你的行数是固定的比U可以做这些没有动态约束..在你的项目

.h文件..

UITableView *tbl; 
int numOfRows; 
float tableViewheight,tableViewWidth,heightForRow; 

在您的项目中.m文件

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
heightForRow=70; 
numOfRows=2; 
tableViewheight=heightForRow*numOfRows; 
tableViewWidth=self.view.frame.size.width; 

tbl=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, tableViewWidth, tableViewheight)]; 
[tbl setDelegate:self]; 
[tbl setDataSource:self]; 
[self.view addSubview:tbl]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return heightForRow; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return numOfRows; 
}