问题
I am trying to do something like this, i have a UIViewControler
. So in my story board i drag and drop a UITableView
to the view. I'm supposed to show few customized rows in that table view. but i want to dynamically change the height of that table view.(According to the row count inside the table view).
i tried like below code, but nothing worked so far.
CGRect tvframe = [tableView frame];
[tableView setFrame:CGRectMake(tvframe.origin.x,
tvframe.origin.y,
tvframe.size.width,
tvframe.size.height + 20)];
and also i have tried this
CGRect frame = self.tableView.frame;
frame.size = self.tableView.contentSize;
self.tableView.frame = frame;
but nothing worked for me.. please some one tell me how do i do this.
thank you.
回答1:
In Autolayout if you want to change the frame then try the constraints (IBOutlet
of NSLayoutConstraint
).

Set the constraint outlets and change constant
value by :
self.sampleConstraint.constant = 20
self.view.layoutIfNeeded()
回答2:
I hope this answer may helps you...
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;
}
The value 150.0 is Static width of label or textview in cell. OR apprx width or horizontal space value of other elements in leading or trailing to the label. Set leading , trailing, top and bottom constraints to the textview in cell don't set static height
Adjust the tableview height to the no. of cell...
- (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];
}];
}

回答3:
CGRect frame = self.tableView.frame;
frame.size.height = resourceArray.count*row_height;
self.tableView.frame = frame;
回答4:
Just use this
tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
回答5:
Try below code,
CGFloat height = self.tableView.rowHeight;
height *= [myArray count];
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = height;
self.tableView.frame = tableViewFrame;
回答6:
Try this :
[UIView animateWithDuration:0.1 animations:^{
CGRect frame = self.tableView.frame;
frame.size.height = 100.0; // expected height
self.tableView.frame = frame;
}];
回答7:
i hope it helps..
if your row count is fixed than u can do these without constraints dynamically..
in your projects .h file..
UITableView *tbl;
int numOfRows;
float tableViewheight,tableViewWidth,heightForRow;
in your projects .m file
- (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;
}
来源:https://stackoverflow.com/questions/30521792/tableview-height-can-not-resize-in-ios