我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
以一种比大多数人投票的答案更紧凑的方式……
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)] && [cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)] && [cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setPreservesSuperviewLayoutMargins:NO];
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
其他回答
以一种比大多数人投票的答案更紧凑的方式……
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)] && [cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)] && [cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setPreservesSuperviewLayoutMargins:NO];
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
对我来说,简单的线条就够了
cell.layoutMargins = UIEdgeInsetsZero
对于iOS 9,你需要添加:
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
详情请参阅问题。
这是在Swift中为我工作的代码:
override func viewDidLoad()
{
super.viewDidLoad()
...
if tableView.respondsToSelector("setSeparatorInset:") {
tableView.separatorInset = UIEdgeInsetsZero
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector("setLayoutMargins:") {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins.left = CGFloat(0.0)
}
}
这对我来说似乎是最干净的(目前),因为所有的单元格/tableView边缘/边缘调整都是在tableView:willDisplayCell:forRowAtIndexPath:方法中完成的,而不是在tableView:cellForRowAtIndexPath:中填充不必要的代码。
顺便说一句,我只是设置单元格的左separatorInset/ layoutmargin,因为在这种情况下,我不想搞砸我在单元格中设置的约束。
代码更新到Swift 2.2:
override func viewDidLoad() {
super.viewDidLoad()
if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
tableView.separatorInset = UIEdgeInsetsZero
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins.left = CGFloat(0.0)
}
}
只需添加下面的代码就可以解决这个程序。
祝你好运!
-(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];
}
}