【问题标题】:TableView Height can not resize in IOSTableView Height 无法在 IOS 中调整大小
【发布时间】:2015-05-29 05:31:37
【问题描述】:

我正在尝试做这样的事情,我有一个UIViewControler。所以在我的故事板中,我将UITableView 拖放到视图中。我应该在该表视图中显示一些自定义行。但我想动态更改该表视图的高度。(根据表视图内的行数)。

我尝试了下面的代码,但到目前为止没有任何效果。

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;

但没有什么对我有用..请有人告诉我该怎么做。

谢谢。

【问题讨论】:

  • 在什么情况下设置表格的框架?
  • 你在使用自动布局吗?
  • @SanjayMohnani 我在 viewDidLoad 事件中使用它
  • @AshishKakkad 是的,我正在使用自动布局
  • @Darshana,请确保您在重新加载事件后设置表格的框架

标签: ios objective-c iphone uitableview


【解决方案1】:

如果您想更改框架,请在自动布局中尝试约束(IBOutlet of NSLayoutConstraint)。

设置约束出口并通过以下方式更改constant值:

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

【讨论】:

  • 好的,你必须为高度添加一个约束。并更改何时将数据填充到表格中。 @Darshana
  • @Darshana 欢迎随时:)
【解决方案2】:

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

对于动态 TableviewCell 高度

- (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 是单元格中标签或文本视图的静态宽度。或在标签前导或尾随其他元素的 apprx 宽度或水平间距值。为单元格中的 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];
}];
}

【讨论】:

    【解决方案3】:
    CGRect frame = self.tableView.frame;
    frame.size.height = resourceArray.count*row_height;
    self.tableView.frame = frame;
    

    【讨论】:

      【解决方案4】:

      就用这个

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

      【讨论】:

        【解决方案5】:

        试试下面的代码,

        CGFloat height = self.tableView.rowHeight;
        height *= [myArray count];
        
        CGRect tableViewFrame = self.tableView.frame;
        tableViewFrame.size.height = height;
        self.tableView.frame = tableViewFrame;
        

        【讨论】:

          【解决方案6】:

          试试这个:

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

          【讨论】:

            【解决方案7】:

            希望对你有帮助..

            如果您的行数是固定的,那么您可以在没有约束的情况下动态执行这些操作..

            在您的项目 .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;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-05-21
              • 2014-09-02
              • 1970-01-01
              • 2018-01-15
              • 2012-07-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多