[英]How to set the full width of separator in UITableView
我有一个UITableView
,其中分隔符没有全宽。 它在左侧前 10 个像素处结束。 我在viewDidLoad()
玩弄这段代码。
self.tableView.layoutMargins = UIEdgeInsetsZero;
同样在故事板中,您可以选择自定义或默认选择器。 现在填充的所有单元格都没有全宽选择器,但空的单元格具有全宽。
我怎样才能解决这个问题?
这在使用 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
我从这篇文章中得到了答案: 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];
}
}
对于 Swift 3 :
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}
我从UITableViewController
继承,并且除了willDisplayCell
的两个 insets 设置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
}
}
适用于 iOS 9+ 中的 Swift
如果使用自定义UITableViewCell
:
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
然后对于viewDidLoad
的UITableView
:
self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
在cellForRowAtIndexPath
方法中使用它,配置单元格的分隔符规格,
它适用于 iOS9.0+
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
对于 iPad 有问题的人——这会让你处于与 iPhone 相同的状态。 然后,您可以根据需要调整separatorInset
。
tableView.cellLayoutMarginsFollowReadableWidth = false
已针对 iOS 9.3 和 Swift 2.2 进行测试。 确保将代码放在willDisplayCell
,该代码在显示单元格之前被调用,而不是放在仅创建单元格的cellForRowAtIndexPath
中。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
为UITableViewController
的函数添加override
,如下所示:
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)
对于边到边分隔符,只需将 left 和 right 的值设置为 0。
顺便将“yourTableView”更改为您的tableView Outlet 名称。
以上在 Swift 2.2 和 Xcode 7.3.1 中都不适合我
结果证明这是最简单的解决方案。 不需要代码。 只需在UITableView
检查器中更改TableViewCell
Layout Margins 值:
对于 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
}
}
在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
和UITableViewCell
的父类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.