uitableviewcell source number example data ios uitableview uikit xcode7 ios9

ios - source - uitableviewcell swift 3



iOS 9 UITableView separadores inserciones(margen izquierdo significativo) (11)

Basado en diferentes respuestas aquí, puedo eliminar el espacio del separador con estas líneas de códigos en Swift:

tableView.separatorInset = UIEdgeInsetsZero tableView.layoutMargins = UIEdgeInsetsZero cell.separatorInset = UIEdgeInsetsZero cell.layoutMargins = UIEdgeInsetsZero

Pero aún estoy teniendo esta pequeña brecha antes del texto:

Tengo un problema con los separadores entre UITableViewCell s en UITableView en iOS 9 . Ellos tienen el margen izquierdo significativo. Ya tengo un código para eliminar el espaciado introducido por iOS 8 pero no funciona con iOS 9 . Parece que agregaron algo más. Supongo que podría estar conectado con layoutMarginsGuide pero todavía no lo he descubierto. ¿Alguien tuvo un problema similar y descubrió la solución?


De acuerdo, he encontrado la solución . Lo único que se necesita para eso es establecer en la instancia de presentación de UITableView ese indicador cellLayoutMarginsFollowReadableWidth

myTableView.cellLayoutMarginsFollowReadableWidth = NO;

Quería encontrar alguna referencia en la documentación, pero parece que todavía no está lista, solo se menciona en la página de diferencias .

Como la bandera se introdujo en iOS 9 para la compatibilidad con versiones anteriores, debe agregar una marca antes de intentar establecerla:

if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) { myTableView.cellLayoutMarginsFollowReadableWidth = NO; }

Para Swift 2.0 puede usar #available para verificar la versión de iOS.

if #available(iOS 9, *) { myTableView.cellLayoutMarginsFollowReadableWidth = false }

Además, debes compilarlo con Xcode 7 o superior.

EDITAR

Tenga en cuenta que esta es la única solución necesaria si sus separadores se ven bien hasta iOS 8, de lo contrario, debe cambiar un poco más. Puede encontrar información sobre cómo hacerlo en SO.


Esta es mi solución para Swift 3.0 / iOS 10 en XCode 8.2.1.

He creado una subclase para UITableview que funciona para IB y crea vistas de tabla mediante programación.

import UIKit class EXCSuperTV: UITableView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupView() } override init(frame: CGRect, style: UITableViewStyle) { super.init(frame: frame, style: style) setupView() } func setupView() {} } class EXCNoFooter: EXCSuperTV { override func setupView() { super.setupView() //tableFooterView = UIView.Zero() } } class EXCMainTV: EXCNoFooter { override func setupView() { super.setupView() separatorInset = UIEdgeInsets.zero } }


Esto funcionó perfectamente para mí en iOS 9.

Para OBJ-C

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) { [tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) { [tableView setLayoutMargins:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } return cell; }


La respuesta aceptada no funcionó para mí. Hasta que moví setCellLayoutMarginsFollowReadableWidth ANTES de setLayoutMargins (todavía necesario para iOS 8):

if([_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) { _tableView.cellLayoutMarginsFollowReadableWidth = NO; } if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) { _tableView.layoutMargins = UIEdgeInsetsZero; }


Para iOS 8 y 9

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero]; if ([UITableView instancesRespondToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO]; }

y

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setLayoutMargins:)]) [cell setLayoutMargins:UIEdgeInsetsZero]; }


Si quieres hacerlo en el constructor de interfaz


Swift 2.2 iOS 9.3

En viewDidLoad

tableView.cellLayoutMarginsFollowReadableWidth = false

En UITableViewDelegates

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if cell.respondsToSelector(Selector("setSeparatorInset:")){ cell.separatorInset = UIEdgeInsetsZero } if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) { cell.preservesSuperviewLayoutMargins = false } if cell.respondsToSelector(Selector("setLayoutMargins:")){ cell.layoutMargins = UIEdgeInsetsZero } }


Solución perfecta hasta iOS 9

En viewDidLoad

- (void)viewDidLoad { [super viewDidLoad]; //Required for iOS 9 if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) { self.testTableView.cellLayoutMarginsFollowReadableWidth = NO; } }

En los métodos TableViewDelegate agregue el siguiente código:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove seperator inset if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } // Prevent the cell from inheriting the Table View''s margin settings if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { [cell setPreservesSuperviewLayoutMargins:NO]; } // Explictly set your cell''s layout margins if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } }


Swift 3.0 / 4.0

tableView.cellLayoutMarginsFollowReadableWidth = false 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: UIView.preservesSuperviewLayoutMargins)) { cell.preservesSuperviewLayoutMargins = false } if cell.responds(to: #selector(setter: UIView.layoutMargins)) { cell.layoutMargins = UIEdgeInsets.zero } }


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove seperator inset if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } // Prevent the cell from inheriting the Table View''s margin settings if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { [cell setPreservesSuperviewLayoutMargins:NO]; } // Explictly set your cell''s layout margins if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } }