Tim*_*giz 142 iphone objective-c uitableview ios
所以我有一个UITableView,其中分隔符没有全宽.它在左侧前10像素结束.我在viewDidLoad中玩这个代码
self.tableView.layoutMargins = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)
也可以在故事板中选择自定义或默认选择器.现在,所有填充的单元格都没有全宽度选择器,但是空单元格具有全宽度.
我怎么能解决这个问题呢?
谢谢你的帮助
MB_*_*per 214
这适用于使用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
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.0更新
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
Run Code Online (Sandbox Code Playgroud)
Tim*_*giz 102
好的,我找到了答案.不知道为什么我以前没有遇到这个帖子,但当然在你发布一个问题之后,突然之后,正确的帖子就会出现在你面前.我从这篇文章得到了答案:iOS 8 UITableView分隔符插入0无法正常工作
只需将此代码添加到您的 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];
}
}
Run Code Online (Sandbox Code Playgroud)
Han*_*ney 95
在你的 UITableViewCell
转到Interface Builder中的Attributes Inspector,只需将"15"更改为0.对要更改的所有单元格执行此操作.
您可能需要添加[cell setLayoutMargins:UIEdgeInsetsZero];到您的tableViewCell
Wil*_*son 27
对于Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}
Run Code Online (Sandbox Code Playgroud)
H6.*_*H6. 19
我继承UITableViewController并需要额外的两个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
}
}
Run Code Online (Sandbox Code Playgroud)
Seb*_*ara 18
UITableViewCellleft和/或right字段.(默认情况下left: 15,right: 0)看看它对我有用(使用left: 100):
结果:
对于有iPad问题的人 - 这将使您处于与iPhone相同的状态.然后,您可以根据需要调整separatorInset.
tableView.cellLayoutMarginsFollowReadableWidth = false
Run Code Online (Sandbox Code Playgroud)
经过iOS 9.3和Swift 2.2测试.确保willDisplayCell在显示单元格之前放置调用的代码,而不是仅在cellForRowAtIndexPath创建单元格的位置.
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
Run Code Online (Sandbox Code Playgroud)
添加override到UITableViewController的函数,如下所示:UITableViewController
适用于iOS 9+中的swift
如果使用自定义UITableViewCell:
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
Run Code Online (Sandbox Code Playgroud)
然后为你UITableView的viewDidLoad:
self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)
在cellForRowAtIndexPath方法中使用它来配置单元格的分隔符规格,
它适用于iOS9.0 +
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
Run Code Online (Sandbox Code Playgroud)
快速 5,Xcode 11 更新
把它放在 viewDidLoad() 里面
yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
Run Code Online (Sandbox Code Playgroud)
对于边到边分隔符,只需将 left 和 right 的值设置为 0。
顺便将“yourTableView”更改为您的tableView Outlet 名称。
以上都不适用于Swift 2.2和Xcode 7.3.1
原来是所有人中最简单的解决方案.无需代码.只需TableViewCell在UITableView检查器中更改布局边距值:

| 归档时间: |
|
| 查看次数: |
80334 次 |
| 最近记录: |