简体   繁体   English

如何设置静态分组的TableView单元格的宽度

[英]How to set static grouped TableView cell's width

I am relatively new to Xcode and Swift development and I have a question regarding a UITableViewController with static, grouped cells (on iPad). 我是Xcode和Swift开发的新手,我有一个关于带有静态分组单元格的UITableViewController的问题(在iPad上)。 I want a natural solution to make the cell width not as big as the whole screen. 我想要一个自然的解决方案,使像元宽度不像整个屏幕那样大。 I have tried this: 我已经试过了:

override func viewDidAppear(_ animated: Bool) {
        tableView.frame = CGRect(x: 175, y: 70, width: view.bounds.width-350, height: view.bounds.height)
}

Problem is that this looks really nasty and gets tricky with navigationbars and tabbars at the top and bottom, but especailly doesn't work properly because it is set at viewDidAppear , so when using the swipe back gesture (like in Settings), the view still appears at full width for a second. 问题是,这看起来确实很讨厌,并且顶部和底部的导航栏和标签栏显得有些棘手,但是由于将其设置为viewDidAppear ,因此有时无法正常工作,因此在使用向后滑动手势时(例如在“设置”中),视图仍然以全角出现一秒钟。

Do you know some other way to solve this issue? 您知道解决此问题的其他方法吗? Maybe some autolayout stuff, i don't know, I've tried everything I know... 也许有些自动布局的东西,我不知道,我已经尝试了所有我知道的东西...

Any help will be appreciated! 任何帮助将不胜感激! Thank you! 谢谢!

Xcode不允许在普通的ViewController中放置tableview分组...

As you say, autolayout is probably the way to go. 正如您所说,自动布局可能是解决之道。 If you are using Interface Builder, then you can just set your constraints there. 如果您使用的是Interface Builder,则可以在那里设置约束。 Plenty of information out there to help with that. 大量的信息可以帮助您解决这一问题。 Here's a good place to start: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html 这是一个不错的起点: https : //developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html

You should switch to using a regular UIViewController and manually add a UITableView as a subview of the root view. 您应该切换为使用常规UIViewController并手动添加UITableView作为根视图的子视图。 You'll also have to manually set the view controller as the delegate and dataSource of the table view. 您还必须手动将视图控制器设置为表视图的委托和数据源。 Then you'll be able to position the table view wherever you like. 然后,您可以将表格视图放置在任意位置。 Unfortunately, you won't be able have static cells - but setting your data dynamically tends to be a better choice in the long run anyway. 不幸的是,您将无法使用静态单元格-但是从长远来看,动态设置数据往往是一个更好的选择。

The other way to achieve what you want and still use a UITableViewController is by setting it as a child view controller, but this is more fiddly and tends to be more restrictive in my experience. 实现您想要的并仍然使用UITableViewController的另一种方法是将其设置为子视图控制器,但这比较麻烦,并且在我的经验中往往会受到更多限制。

If you want to manually set frames as in your code example, then you should do that inside the viewDidLayoutSubviews method. 如果要像代码示例中一样手动设置框架,则应在viewDidLayoutSubviews方法内进行设置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM