zoukankan      html  css  js  c++  java
  • 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题

    一、问题描述

    UITableView分割线要显示到最左端

    查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置。

    @property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators

    打印separatorInset,其默认值{0, 15, 0, 0},上、左、下、右距离原位置分别为0、15、0、0,即左侧会有默认15像素的空白

    全局设置每个cell的separatorInset值。在UITableViewController的-(void)viewDidLoad方法中设置UITableView分割线,UIEdgeInsetsZero相当于UIEdgeInsetsMake(0, 0, 0, 0)。

     1 -(void)viewDidLoad
     2 {
     3     //设置分割线距边界的距离
     4     //在iOS7之前没有separatorInset,运行会导致程序崩溃,所以要加下判断
     5     //if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
     6     //{
     7     //    self.tableView.separatorInset = UIEdgeInsetsZero;
     8     //}
     9     if([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
    10     {
    11         self.tableView.separatorInset = UIEdgeInsetsZero;
    12     }
    13 }

    或者单独每个cell的separatorInset值。在UITableViewController的-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath方法中设置UITableView分割线,UIEdgeInsetsZero相当于UIEdgeInsetsMake(0, 0, 0, 0)。

     1 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
     2 {
     3     //设置分割线距边界的距离
     4     //在iOS7之前没有separatorInset,运行会导致程序崩溃,所以要加下判断
     5     //if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
     6     //{
     7     //    cell.separatorInset = UIEdgeInsetsZero;
     8     //}
     9     if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
    10         cell.separatorInset = UIEdgeInsetsZero;
    11     }
    12 }

    运行,发现达不到我想要的效果,分割线只是距离最左端近了,但还是没显示到最左端,效果图如下:

    二、问题分析

    iOS7,想要设置cell的分割线显示到最左端,只需要设置separatorInset的值为UIEdgeInsetsZero

    iOS8,简单设置separatorInset的值为UIEdgeInsetsZero的方法已经无效了。UIView的layoutMargins 默认为{8, 8, 8, 8}。

    cell的preservesSuperviewLayoutMargins默认为true时,可能会导致cell被其父UITableView的LayoutMargin影响。如果设置为false时,cell不被UITableView的LayoutMargin影响。

    三、问题解决

    1.方法一:

    全局设置cell的separatorInset的值为UIEdgeInsetsZero,UITableView的layoutMargins设置为UIEdgeInsetsZero,并且cell的layoutMargins设置为UIEdgeInsetsZero。

     1 -(void)viewDidLoad
     2 {
     3     //设置分割线距边界的距离
     4     //在iOS7之前没有separatorInset,运行会导致程序崩溃,所以要加下判断
     5     //if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
     6     //{
     7     //    self.tableView.separatorInset = UIEdgeInsetsZero;
     8     //}
     9     if([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
    10     {
    11         self.tableView.separatorInset = UIEdgeInsetsZero;
    12     }
    13     if([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
    14     {
    15         self.tableView.layoutMargins = UIEdgeInsetsZero;
    16     }
    17 }
    1 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    2 {
    3     if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    4         cell.layoutMargins = UIEdgeInsetsZero;
    5     }
    6 }

    2.方法二:

    preservesSuperviewLayoutMargins默认为true,cell被UITableView的layoutMargins影响。

    设置cell的separatorInset值为UIEdgeInsetsZero,cell的layoutMargins值为UIEdgeInsetsZero,并且cell的preservesSuperviewLayoutMargins为false时,避免cell被UITableView的layoutMargins影响。

     1 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
     2 {
     3     if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
     4         cell.separatorInset = UIEdgeInsetsZero;
     5     }
     6     if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
     7         cell.layoutMargins = UIEdgeInsetsZero;
     8     }
     9     //preservesSuperviewLayoutMargins设置为false时,子view不被其父view的LayoutMargin影响
    10     if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
    11         cell.preservesSuperviewLayoutMargins = false;
    12     }
    13 }

    最终效果:

    四、存在问题

    该方法设置cell的分割线,在竖屏下显示正常,在横屏下会无效。网上查阅,发现解决cell分割线的方法都存在着这个问题

    五、饮水思源

    1.http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working

    2.http://dev.classmethod.jp/smartphone/iphone/ios-8-uitableview-layoutmargins/

    3.http://www.cnblogs.com/Alex-798-Dcr/p/5279920.html

    4.http://www.skyfox.org/ios7-tableview-separatorinset-ajust.html

    学习,以记之。如有错漏,欢迎指正

    作者:冯子武
    出处:http://www.cnblogs.com/Zev_Fung/
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处。
    如果博文对您有所收获,请点击下方的 [推荐],谢谢

  • 相关阅读:
    List of the best open source software applications
    Owin对Asp.net Web的扩展
    NSwag给api加上说明
    'workspace' in VS Code
    unable to find valid certification path to requested target
    JMeter的下载以及安装使用
    exception disappear when forgot to await an async method
    Filter execute order in asp.net web api
    记录web api的request以及response(即写log)
    asp.net web api的源码
  • 原文地址:https://www.cnblogs.com/Zev_Fung/p/5650922.html
Copyright © 2011-2022 走看看