简体   繁体   English

iOS-如何在TableViewController中设置TableView的宽度?

[英]iOS - How to set width of TableView in TableViewController?

using a TableViewController, how can I resize the length of the cells within it? 使用TableViewController,如何调整其中的单元格的长度?

As you can see, the length of the cells is extended to the entire display. 如您所见,单元格的长度扩展到整个显示。 How can I reduce it? 我该如何减少呢?

Try this: 尝试这个:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES]
    [self.tableView setFrame:CGRectMake(x, y, width, height)];
}

try this 尝试这个

Chanage the frame in viewWillAppear viewWillAppear更改框架

- (void)viewWillAppear:(BOOL)animated
{
     self.view.frame = CGRectMake(0, 0, 200, 200);
    [super viewWillAppear:animated];
}

or change in 或改变

Called to notify the view controller that its view is about to layout its subviews.When a view's bounds change, the view adjusts the position of its subviews. 调用此方法以通知视图控制器其视图将要布局其子视图。当视图的边界发生变化时,视图将调整其子视图的位置。

So instead of doing it in view will appear,over ride the willLayoutSubviews : 因此,不是在视图中显示它,而是在willLayoutSubviews中运行

-(void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

      self.view.frame = CGRectMake(0, 0, 200, 200);

}

or efficient way 或有效的方法

- (void)viewDidAppear:(BOOL)animated
{
 [super viewDidAppear:YES]
[self.tableView setFrame:CGRectMake(x, y, 200, 200)];
}

swift3 迅捷3

Change the frame in viewWillAppear 更改viewWillAppear的框架

override func viewWillAppear(_ animated: Bool) {
self.view.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(200), height: CGFloat(200))
super.viewWillAppear(animated)
}

or change in 或改变

Called to notify the view controller that its view is about to layout its subviews.When a view's bounds change, the view adjusts the position of its subviews. 调用此方法以通知视图控制器其视图将要布局其子视图。当视图的边界发生变化时,视图将调整其子视图的位置。

So instead of doing it in view will appear,over ride the willLayoutSubviews : 因此,不是在视图中显示它,而是在willLayoutSubviews中运行

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.view.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(200), height: CGFloat(200))
}

or efficient way 或有效的方法

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
self.tableView.frame = CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(200), height: CGFloat(200))
}

Why do you need to change the width? 为什么需要更改宽度?

  • If you want to put another view at the table view left or right, try UISplitviewController. 如果要在表视图的左侧或右侧放置另一个视图,请尝试UISplitviewController。
  • if you want the cell margins inside table view, I would suggest to subclass UITableViewCell and set the width in the following way: 如果要在表格视图中使用单元格边距,建议使用以下方法子类化UITableViewCell并设置宽度:
class MyTableViewCell: UITableViewCell {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.frame.size.width = 200
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS - 如何将视图宽度设置为等于TableView分隔符宽度 - iOS - How to set View width equal to TableView Separator width 无法在TableViewController中为tableview设置约束 - not able to set contraints for tableview in TableViewController 如何在TableView中使用TableViewController类? - How to use TableViewController Class in TableView? 如何将TableViewController与另一个TableView链接 - How to link a TableViewController with another TableView 如何在TableViewController上向TableView添加约束? - How to add constraints to TableView on TableViewController? 如果当前视图为tableViewController,iOS TableView将不会重新加载 - iOS TableView not reloading if current view is tableViewController iOS底部对齐TableViewController中的TableView(StoryBoard) - iOS bottom align a TableView in TableViewController (StoryBoard) iOS TableViewController设置状态栏颜色 - iOS TableViewController set statusbar color 如何在Swift 4 IOS中通过TableViewController的“ viewForHeaderInSection”方法设置UITableViewHeaderFooterView的属性? - How to set the property of UITableViewHeaderFooterView from “viewForHeaderInSection” method of TableViewController in swift 4 IOS? 如何在TableViewController的tableview上方添加图像-Swift 3 - How to add an image above tableview in TableViewController - Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM