我有一个 UITableView
,其中分隔符没有全宽。它在左侧前 10 个像素处结束。我在 viewDidLoad()
中玩弄这段代码。
self.tableView.layoutMargins = UIEdgeInsetsZero;
同样在情节提要时,您可以选择自定义或默认选择器。现在所有填充的单元格都没有全宽选择器,但空的单元格有全宽。
我怎样才能解决这个问题?
UITableView
有一个属性 separatorInset
。将UITableView
行分隔符的插入设置为零。您也可以从情节提要中更改 separatorInset
这适用于使用 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
}
斯威夫特 5 更新:
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
在您的 UITableViewCell
转到 Interface Builder 中的 Attributes Inspector,只需将“15”更改为 0。对您希望更改的所有单元格执行此操作。
https://i.stack.imgur.com/A8rY1.png
您可能需要将 [cell setLayoutMargins:UIEdgeInsetsZero];
添加到您的 tableViewCell
我从这篇文章中得到了答案:iOS 8 UITableView separator inset 0 not working
只需将此代码添加到您的 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];
}
}
viewDidLayoutSubviews
不是必需的
对于斯威夫特 3:
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}
选择您的 UITableViewCell 转到属性检查器 转到分隔符并将其更改为“自定义插入”设置左侧和/或右侧字段。 (默认左:15,右:0)
看看它是如何工作的(使用 left: 100
):
https://i.stack.imgur.com/zlC8T.png
结果:
https://i.stack.imgur.com/Uuv0N.png
我从 UITableViewController
继承,并且还需要在 willDisplayCell
中的两个插图设置之外将 preservesSuperviewLayoutMargins
设置为 false。在 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
}
}
preservesSuperviewLayoutMargins = false
因为默认值已经是 false
分隔符 Inset
默认为左起 15。将分隔符 Inset
选项从 auto
更改为 custom
并将插入设置为 0
。
https://i.stack.imgur.com/l40TC.png
对于 iOS 9+ 中的 Swift
如果使用自定义 UITableViewCell
:
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
然后对于 viewDidLoad
中的 UITableView
:
self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
对于 iPad 有问题的人——这将使您处于与 iPhone 相同的状态。然后,您可以根据需要调整 separatorInset
。
tableView.cellLayoutMarginsFollowReadableWidth = false
在 cellForRowAtIndexPath
方法中使用它来配置单元格的分隔符规格,
它非常适合 iOS9.0+
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
测试 iOS 9.3 &斯威夫特 2.2。确保将代码放在在显示单元格之前调用的 willDisplayCell
中,而不是在您仅创建单元格的 cellForRowAtIndexPath
中。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
将 override
添加到 UITableViewController
的函数中,如下所示:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
斯威夫特 5,Xcode 11 更新
把它放在 viewDidLoad() 里面
yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
对于边到边分隔符,只需将左右的值设置为 0。
顺便将“yourTableView”更改为您的 tableView 插座名称。
UIEdgeInsets.zero
以上方法在 Swift 2.2 和 Xcode 7.3.1 中都不适合我
原来是最简单的解决方案。无需代码。只需在 UITableView
检查器中更改 TableViewCell
Layout Margins 值:
https://i.stack.imgur.com/0TC8z.png
对于斯威夫特 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
}
}
在 viewDidLoad
(测试 iOS11 - swift 4.1)
尝试
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
这些解决方案都不适用于 iPad,但我想出了一个涵盖这两种设备的解决方案:
使用可重复使用的细胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
...[other code]...
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
return cell;
}
使用不可重复使用的细胞:
- (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];
}
只是为了扩展这种方法:
@property(nonatomic) UIEdgeInsets separatorInset;
@property(nonatomic) UIEdgeInsets layoutMargins;
UITableView
& 可以使用这两个属性UITableViewCell
。后者实际上是 UIView
的一个属性,它是 UITableView
和 UITableView
的父类。 UITableViewCell
。
不定期副业成功案例分享