[英]How to set the full width of separator in UITableView
I have a UITableView
where the separators don't have the full width.我有一个
UITableView
,其中分隔符没有全宽。 It ends like 10 pixels before the left side.它在左侧前 10 个像素处结束。 I was playing around with this code in the
viewDidLoad()
.我在
viewDidLoad()
玩弄这段代码。
self.tableView.layoutMargins = UIEdgeInsetsZero;
Also in the storyboard when you can select custom or default selectors.同样在故事板中,您可以选择自定义或默认选择器。 Now all the cells that are populated don't have the full-width selectors but the cells that are empty have full width.
现在填充的所有单元格都没有全宽选择器,但空的单元格具有全宽。
How can I fix this?我怎样才能解决这个问题?
This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:这在使用 Xcode 6.4 和 Swift 1.2 的 iOS 8.4 - 9.0 设备上对我有用:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
Swift 5 Update:斯威夫特 5 更新:
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
I got the answer from this post: iOS 8 UITableView separator inset 0 not working我从这篇文章中得到了答案: iOS 8 UITableView separator inset 0 not working
Just add this code on your UITableViewController
只需在您的
UITableViewController
上添加此代码
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
In your UITableViewCell
在你的
UITableViewCell
Go to Attributes Inspector in your Interface Builder and simply change "15" to 0. Do this for all the cells you wish to change.转到界面生成器中的 Attributes Inspector 并将“15”更改为 0。对您希望更改的所有单元格执行此操作。
You may need to add [cell setLayoutMargins:UIEdgeInsetsZero];
您可能需要添加
[cell setLayoutMargins:UIEdgeInsetsZero];
to your tableViewCell
到您的
tableViewCell
for Swift 3 :对于 Swift 3 :
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}
UITableViewCell
UITableViewCell
left
and / or right
fields.left
和/或right
字段。 (By default left: 15
, right: 0
) left: 15
, right: 0
) Look how it works (using left: 100
):看看它是如何工作的(使用
left: 100
):
Result:结果:
I inherit from UITableViewController
and needed additional to the two insets settings in willDisplayCell
also to set preservesSuperviewLayoutMargins
to false.我从
UITableViewController
继承,并且除了willDisplayCell
的两个 insets 设置willDisplayCell
还需要将preservesSuperviewLayoutMargins
设置为 false。 In Swift it looks like this:在 Swift 中,它看起来像这样:
override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
}
For Swift in iOS 9+适用于 iOS 9+ 中的 Swift
If using a custom UITableViewCell
:如果使用自定义
UITableViewCell
:
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
then for your UITableView
in viewDidLoad
:然后对于
viewDidLoad
的UITableView
:
self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
Use it in cellForRowAtIndexPath
method, to configure cell's separator specs ,在
cellForRowAtIndexPath
方法中使用它,配置单元格的分隔符规格,
it works perfect for iOS9.0+它适用于 iOS9.0+
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
For people having issues with the iPad — this will get you in a state that is the same as the iPhone.对于 iPad 有问题的人——这会让你处于与 iPhone 相同的状态。 You can then adjust the
separatorInset
as needed.然后,您可以根据需要调整
separatorInset
。
tableView.cellLayoutMarginsFollowReadableWidth = false
Tested for iOS 9.3 & Swift 2.2.已针对 iOS 9.3 和 Swift 2.2 进行测试。 Make sure to put the code in
willDisplayCell
which is called just before displaying the cell and not in cellForRowAtIndexPath
where you create the cell only.确保将代码放在
willDisplayCell
,该代码在显示单元格之前被调用,而不是放在仅创建单元格的cellForRowAtIndexPath
中。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
Add override
to the function for UITableViewController
, like so:为
UITableViewController
的函数添加override
,如下所示:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
swift 5, Xcode 11 Update快速 5,Xcode 11 更新
place this inside viewDidLoad()把它放在 viewDidLoad() 里面
yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
for edge to edge separator just set the value of left and right to 0.对于边到边分隔符,只需将 left 和 right 的值设置为 0。
by the way change the "yourTableView" to your tableView Outlet name.顺便将“yourTableView”更改为您的tableView Outlet 名称。
None of the above worked for me in Swift 2.2 and Xcode 7.3.1以上在 Swift 2.2 和 Xcode 7.3.1 中都不适合我
Turned out to be the simplest solution of all.结果证明这是最简单的解决方案。 No code needed.
不需要代码。 Just change
TableViewCell
Layout Margins values in your UITableView
inspector:只需在
UITableView
检查器中更改TableViewCell
Layout Margins 值:
For Swift 3 :对于 Swift 3 :
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
}
In viewDidLoad
(tested iOS11 - swift 4.1)在
viewDidLoad
(测试 iOS11 - swift 4.1)
try尝试
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
None of these solutions work on the iPad, but I have come up with a solution that covers both devices:这些解决方案都不适用于 iPad,但我提出了一个涵盖两种设备的解决方案:
With reusable cells:使用可重复使用的细胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
...[other code]...
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
return cell;
}
With non reusable cells:对于不可重复使用的单元格:
- (void)removeSeparatorInset:(UITableView*)tableView{
NSArray *cells = [tableView visibleCells];
for (UITableViewCell *cell in cells){
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
-(void) viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self removeSeparatorInset:self.tableView];
}
Just to expand on this approach:只是为了扩展这种方法:
@property(nonatomic) UIEdgeInsets separatorInset;
@property(nonatomic) UIEdgeInsets layoutMargins;
Both properties can be used by UITableView
& UITableViewCell
. UITableView
和UITableViewCell
可以使用这两个属性。 The latter is, in fact, a property of UIView
, which is a parent class of both UITableView
& UITableViewCell
.后者实际上是
UIView
一个属性,它是UITableView
和UITableViewCell
的父类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.