iOS 8 wstawka separatora UITableView 0 nie działa

Mam aplikację, w której wkładka separatora UITableView jest ustawiona na wartości niestandardowe-Right 0, Left 0. Działa to doskonale w iOS 7.x, jednak w iOS 8.0 widzę, że wstawka separatora jest ustawiona na domyślną wartość 15 po prawej stronie. Mimo że w plikach xib ustawiona jest na 0, nadal wyświetla się nieprawidłowo.

Jak usunąć marginesy separatora UITableViewCell?

Author: Shobhakar Tiwari, 2014-09-10

30 answers

IOS 8.0 wprowadza właściwość layoutMargins w widokach komórek i tabeli.

Ta właściwość nie jest dostępna na iOS 7.0, więc musisz upewnić się, że sprawdzisz ją przed przypisaniem!

Dodatkowo firma Apple dodała do komórki właściwość , która uniemożliwi dziedziczenie ustawień marginesu widoku tabeli. Gdy ta właściwość jest ustawiona, komórki mogą konfigurować własne marginesy niezależnie od widoku tabeli. Pomyśl o tym jak o override.

Ta właściwość jest wywoływana preservesSuperviewLayoutMargins, a ustawienie jej na NO pozwoli komórce layoutMargin nadpisać cokolwiek layoutMargin jest ustawione w widoku TableView. Pozwala to oszczędzić czas ( nie trzeba modyfikować ustawień widoku tabeli ) i jest bardziej zwięzły. Proszę odnieść się do odpowiedzi Mike Abdullah szczegółowe wyjaśnienie.

uwaga: poniżej znajduje się czysta implementacja dla ustawienia marginesu na poziomie komórki , wyrażona w odpowiedzi Mike ' a Abdullaha. Ustawienie preservesSuperviewLayoutMargins=NO zapewni, że widok tabeli nie nadpisuje ustawień komórki. Jeśli chcesz, aby cały widok tabeli miał spójne marginesy, dostosuj odpowiednio kod.

Ustaw marginesy komórki:

-(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 4:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // Remove seperator inset
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
    // Explictly set your cell's layout margins
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = .zero
    }
}

Ustawienie właściwości preservesSuperviewLayoutMargins w komórce na NO powinno zapobiec nadpisywaniu marginesów w widoku tabeli. W w niektórych przypadkach wydaje się, że nie działa prawidłowo.

Jeśli wszystko się nie powiedzie, możesz wymusić marginesy widoku tabeli:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    // Force your tableview margins (this may be a bad idea)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
} 

Swift 4:

func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // Force your tableview margins (this may be a bad idea)
    if tableView.responds(to: #selector(setter: UITableView.separatorInset)) {
        tableView.separatorInset = .zero
    }
    if tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {
        tableView.layoutMargins = .zero
    }
}

...no i proszę! Powinno to działać na iOS 7 i 8.

Edytuj: Mohamed Saleh zwrócił moją uwagę na możliwą zmianę w iOS 9. Może być konieczne ustawienie widoku tabeli cellLayoutMarginsFollowReadableWidth na NO, jeśli chcesz dostosować wstawki lub marginesy. Przebieg może się różnić, nie jest to udokumentowane dobrze.

Ta właściwość istnieje tylko w systemie iOS 9, więc sprawdź ją przed ustawieniem.

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

Swift 4:

if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {
    myTableView.cellLayoutMarginsFollowReadableWidth = false
}

(powyższy kod z iOS 8 wstawka separatora UITableView 0 nie działa )

EDIT: Oto podejście do tworzenia interfejsów:

TableViewAttributesInspector TableViewCellSizeInspector

Uwaga: iOS 11 zmienia i upraszcza wiele z tego zachowania, aktualizacja będzie dostępna...

 1053
Author: cdstamper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-07 13:04:12

Arg!!! Po zabawie albo robi to w Twojej Cell podklasie:

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}

Lub ustawienie cell.layoutMargins = UIEdgeInsetsZero; naprawiło to dla mnie.

 251
Author: user3570727,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-03 05:35:31

Poświęćmy chwilę, aby zrozumieć problem, zanim ślepo ładujemy się, aby spróbować go naprawić.

Szybkie przeszukanie w debuggerze powie Ci, że linie separatorów są podglądami podrzędnymi UITableViewCell. Wydaje się, że sama komórka ponosi sporą odpowiedzialność za układ tych linii.

IOS 8 wprowadza koncepcję marginesów układu . Domyślnie marginesy układu widoku są 8pt Po wszystkich stronach i są dziedziczone od przodka widoki.

Jak najlepiej możemy powiedzieć, układając linię separatora, UITableViewCell decyduje się przestrzegać lewego marginesu układu, używając go do ograniczenia lewej wstawki.

Aby osiągnąć pożądany wkład prawdziwie zerowy, musimy:]}
  • Ustaw lewy margines układu na 0
  • Zatrzymaj wszelkie dziedziczone marginesy, które są nadrzędne

Mówiąc w ten sposób, to dość proste zadanie do osiągnięcia:

cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

Rzeczy do Uwaga:

  • ten kod tylko musi być uruchamiany raz na komórkę( w końcu konfigurujesz właściwości komórki) i nie ma nic specjalnego w tym, gdy zdecydujesz się go wykonać. Rób to, co wydaje ci się najczystsze.
  • niestety żadna właściwość nie jest dostępna do skonfigurowania w Interface Builder, ale możesz określić zdefiniowany przez użytkownika atrybut runtime dla preservesSuperviewLayoutMargins w razie potrzeby.
  • oczywiście, jeśli Twoja aplikacja jest przeznaczona dla wcześniejszych wersji systemu operacyjnego, musisz unikać wykonywania powyższy kod do uruchomienia na iOS 8 i nowszych.
  • zamiast ustawiać preservesSuperviewLayoutMargins, możesz skonfigurować widoki przodków (takie jak tabela) tak, aby miały 0 lewy margines, ale wydaje się to z natury bardziej podatne na błędy, ponieważ nie kontrolujesz całej hierarchii.
  • prawdopodobnie byłoby nieco czystsze ustawienie tylko lewego marginesu na {[4] } i pozostawienie pozostałych.
  • Jeśli chcesz mieć wstawkę 0 Na "dodatkowych" separatorach, które UITableView rysują na dole zwykłego stylu tabele, zgaduję, że będzie to wymagało określenia tych samych ustawień na poziomie tabeli zbyt (nie próbowałem tego!)
 175
Author: Mike Abdullah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-09 10:49:22

Uważam, że jest to to samo pytanie, które zadałem tutaj: Usuń SeparatorInset na iOS 8 UITableView dla Xcode 6 iPhone Simulator

W iOS 8 istnieje jedna nowa właściwość dla wszystkich obiektów dziedziczonych z UIView. Tak więc rozwiązanie, aby ustawić SeparatorInset w iOS 7.x nie będzie w stanie usunąć białej spacji widocznej w widoku UITableView w systemie iOS 8.

Nowa właściwość nazywa się " layoutMargins ".

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

iOS 8 UITableView setSeparatorInset: uiedgeinsetszero setLayoutMargins:UIEdgeInsetsZero

The rozwiązanie: -

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(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];
   }
}
Jeśli ustawisz cell.layoutMargins = UIEdgeInsetsZero; bez sprawdzania, czy layoutMargins istnieje, aplikacja zawiesi się na iOS 7.x. najlepszym sposobem byłoby sprawdzenie, czy layoutMargins istnieje najpierw przed setLayoutMargins:UIEdgeInsetsZero.
 55
Author: Ricky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:43

Możesz użyć UIAppearance raz, podczas uruchamiania aplikacji (przed załadowaniem interfejsu), aby ustawić go jako domyślne ustawienia globalne:

// iOS 7:
[[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];

[[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];

// iOS 8:
if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {

    [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];

}

W ten sposób utrzymujesz czysty kod Uiviewcontrollera i zawsze możesz go zastąpić, jeśli chcesz.

 44
Author: Lukasz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-05 17:31:17

Wersja Swift

IOS wprowadza właściwość layoutMargins w widokach komórek i tabeli.

Ta właściwość nie jest dostępna w systemie iOS 7.0, więc musisz sprawdzić ją przed przypisaniem!

Jednak Apple dodał właściwość o nazwie preservesSuperviewLayoutMargins do Twojej komórki, która uniemożliwi jej dziedziczenie ustawień marginesu widoku tabeli. W ten sposób komórki mogą skonfigurować własne marginesy niezależnie od widoku tabeli. Potraktuj to jako obejście.

Ta właściwość nazywa się preservesSuperviewLayoutMargins i ustawienie jej NA NO pozwala na nadpisanie ustawień layoutMargin widoku tabeli ustawieniami layoutMargin Twojej komórki. Pozwala to zaoszczędzić czas ( nie trzeba modyfikować ustawień widoku tabeli ) i jest bardziej zwięzły. Proszę odnieść się do odpowiedzi Mike Abdullah szczegółowe wyjaśnienie.

uwaga: jest to właściwa, mniej bałaganiarska implementacja, wyrażona w Mike Abdullah ' s answer; ustawienie preservesSuperviewLayoutMargins = NO zapewni, że widok tabeli nie nadpisuje ustawień komórki.

Pierwszy krok-Ustaw marginesy komórki:

/*
    Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath)
{
    // Remove separator inset
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }

    // Explictly set your cell's layout margins
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
}

Ustawienie właściwości preservesSuperviewLayoutMargins w komórce na NO powinno zapobiec nadpisywaniu marginesów w widoku tabeli. W niektórych przypadkach wydaje się, że nie działa prawidłowo.

Drugi krok - tylko jeśli wszystko zawiedzie, możesz brute-force marginesy widoku tabeli:

/*
    Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Force your tableview margins (this may be a bad idea)
    if self.tableView.respondsToSelector("setSeparatorInset:") {
        self.tableView.separatorInset = UIEdgeInsetsZero
    }

    if self.tableView.respondsToSelector("setLayoutMargins:") {
        self.tableView.layoutMargins = UIEdgeInsetsZero
    }
}

...no i proszę! Powinno to działać na iOS 8, a także iOS 7.

Uwaga: testowane przy użyciu iOS 8.1 i 7.1, w moim przypadku musiałem tylko użyć pierwszego kroku tego wyjaśnienia.

Drugi krok jest wymagany tylko wtedy, gdy masz niepopulowaną komórkę pod renderowanymi komórkami, tj. jeśli tabela jest większa niż liczba wierszy w modelu tabeli. Nie wykonanie drugiego kroku skutkowałoby różnymi przesunięciami separatora.

 39
Author: King-Wizard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-19 07:46:25

W Swifcie jest to nieco bardziej irytujące, ponieważ layoutMargins jest właściwością, więc musisz nadpisać getter i setter.

override var layoutMargins: UIEdgeInsets {
  get { return UIEdgeInsetsZero }
  set(newVal) {}
}

To skutecznie uczyni layoutMargins tylko czytelnym, co w moim przypadku jest w porządku.

 35
Author: swilliams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-06 20:15:15

Dla iOS 9 musisz dodać:

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

Aby uzyskać więcej informacji, zapoznaj się z pytaniem .

 21
Author: Julian Król,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:19

Swift 2.0 Extension

Chciałem udostępnić rozszerzenie, które zrobiłem, aby usunąć marginesy z separatorów komórek tableview.

extension UITableViewCell {
    func removeMargins() {

        if self.respondsToSelector("setSeparatorInset:") {
            self.separatorInset = UIEdgeInsetsZero
        }

        if self.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
            self.preservesSuperviewLayoutMargins = false
        }

        if self.respondsToSelector("setLayoutMargins:") {
            self.layoutMargins = UIEdgeInsetsZero
        }
    }
}

Używane w kontekście:

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell

    cell.removeMargins()
    return cell
 18
Author: Dan Beaulieu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-09 16:55:31

Swift:

override func viewDidLoad() {
    super.viewDidLoad()

    if self.tableView.respondsToSelector("setSeparatorInset:") {
        self.tableView.separatorInset = UIEdgeInsetsZero
    }
    if self.tableView.respondsToSelector("setLayoutMargins:") {
        self.tableView.layoutMargins = UIEdgeInsetsZero
    }

    self.tableView.layoutIfNeeded()            // <--- this do the magic
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     ...

    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }

    return cell
}
 13
Author: Alexander Volkov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-01 15:20:32

Udało mi się to zrobić:

tableView.separatorInset = UIEdgeInsetsZero;
tableView.layoutMargins = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
 10
Author: ideawu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-30 10:19:56

Przykład Swift 3.0:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // removing seperator inset
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    // prevent the cell from inheriting the tableView's margin settings
    if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
    // explicitly setting cell's layout margins
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = .zero
    }
}
 9
Author: mkz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-03 12:33:46

Po długim śledztwie...

Oto jedyny sposób, aby w pełni kontrolować te rzeczy (które mogłem znaleźć)

Aby w pełni kontrolować zarówno wstawki separatora, jak i marginesy układu na każdej komórce. Zrób to metodą willDisplayCell na swoim UITableviewDelegate.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layoutMargins = UIEdgeInsetsZero
    cell.contentView.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10)
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}

Obiekt cell kontroluje separator, a contentView kontroluje wszystko inne. Jeśli spacje w separatorze są wyświetlane w nieoczekiwanym kolorze, powinno to rozwiązać:

cell.backgroundColor = cell.contentView.backgroundColor
 9
Author: Matjan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-12 16:12:09

Dla mnie prosta linia wykonała zadanie

cell.layoutMargins = UIEdgeInsetsZero
 8
Author: apinho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-18 10:30:47

Co do tego, co cdstamper zasugerował zamiast widoku tabeli, dodanie poniższych linii w komórce layoutsubview działa dla mnie.

- (void)layoutSubviews 
{
    [super layoutSubviews];

    if ([self respondsToSelector:@selector(setSeparatorInset:)])
                [self setSeparatorInset:UIEdgeInsetsZero];

        if ([self respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
        {
            [self setPreservesSuperviewLayoutMargins:NO];;
        }

        if ([self respondsToSelector:@selector(setLayoutMargins:)]) 
        {
            [self setLayoutMargins:UIEdgeInsetsZero];
        }
}
 8
Author: Vijay V S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-14 07:40:20

Proste rozwiązanie w Swift dla iOS 8 z niestandardowym UITableViewCell

override func awakeFromNib() {
    super.awakeFromNib()

    self.layoutMargins = UIEdgeInsetsZero
    self.separatorInset = UIEdgeInsetsZero
}

W ten sposób ustawiasz layoutMargin i separatorInset tylko jeden raz zamiast robić to dla każdego willDisplayCell, Jak sugeruje większość powyższych odpowiedzi.

Jeśli używasz niestandardowego UITableViewCell to jest właściwe miejsce, aby to zrobić. W przeciwnym razie należy to zrobić w tableView:cellForRowAtIndexPath.

Kolejna podpowiedź: nie musisz ustawiać preservesSuperviewLayoutMargins = false, ponieważ domyślną wartością jest już NO!

 7
Author: Francesco Vadicamo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-04 09:24:28

Wystarczy dodać poniższy kod, aby rozwiązać ten program.

Powodzenia!
-(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];
       }

}
 7
Author: JimmyChang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-18 08:27:07

Użyj poniższego fragmentu kodu unikaj niechcianego problemu z wypełnieniem dla UITableView w systemie iOS 8 i 7.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(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];
    }
}
 4
Author: Anooj VM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-03 12:24:03

To jest kod, który działa dla mnie, w 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)
    }
}

Wydaje mi się to najczystsze (na razie), ponieważ wszystkie korekty krawędzi/marginesu cell/tableView są wykonywane w metodzie tableView:willDisplayCell:forRowAtIndexPath:, bez umieszczania kodu unneccessary w tableView:cellForRowAtIndexPath:.

Btw, ustawiam tylko lewy separator komórki / layoutMargins, bo w tym przypadku nie chcę spieprzyć moich ograniczeń, które ustawiłem w komórce.

Kod zaktualizowany do 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)
        }
    }
 4
Author: Ivan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-22 09:07:35

Większość odpowiedzi pokazuje wstawki separatorów i marginesy układu ustawiane w różnych metodach (np., viewDidLayoutSubviews, willDisplayCell, etc) dla komórek i tableviews, ale odkryłem, że samo umieszczenie ich w cellForRowAtIndexPath działa świetnie. Wygląda na najczystszy sposób.

// kill insets for iOS 8
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
    cell.preservesSuperviewLayoutMargins = NO;
    [cell setLayoutMargins:UIEdgeInsetsZero];
}
// iOS 7 and later
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    [cell setSeparatorInset:UIEdgeInsetsZero];
 3
Author: inorganik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-29 17:21:13

Zamiast aktualizować preservesSuperviewLayoutMargins i layoutMargins za każdym razem, gdy komórka się przewija (używając willDisplayCell), proponuję zrobić to raz w cellForRowAtIndexPath::

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)

    cell.preservesSuperviewLayoutMargins = false
    cell.layoutMargins = UIEdgeInsetsZero

    return cell

}
 3
Author: Rudolf Adamkovič,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-01 10:23:21

Lukasz odpowiedz W Swift:

    // iOS 7:
    UITableView.appearance().separatorStyle = .SingleLine
    UITableView.appearance().separatorInset = UIEdgeInsetsZero
    UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero

    // iOS 8:
    if UITableView.instancesRespondToSelector("setLayoutMargins:") {
        UITableView.appearance().layoutMargins = UIEdgeInsetsZero
        UITableViewCell.appearance().layoutMargins = UIEdgeInsetsZero
        UITableViewCell.appearance().preservesSuperviewLayoutMargins = false
    }
 3
Author: Warthog,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-20 20:47:48

W iOS8:

Dodanie tego do mojej podklasy UITableViewCell:

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

I to do "tableView: cellForRowAtIndexPath" lub "tableView: willDisplayCell":

[editCell setSeparatorInset:UIEdgeInsetsZero];
Zadziałało dla mnie.
 3
Author: CoderSaru,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-10 11:18:38

Oto prosty sposób na globalne usunięcie wstawki.

W UITableViewCell+Extensions.swift:

import UIKit

extension UITableViewCell {

  override public var layoutMargins: UIEdgeInsets {
    get { return UIEdgeInsetsZero }
    set { }
  }

}

W AppDelegate application:didFinishLaunchingWithOptions::

  UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero

Można by pomyśleć, że a) po prostu nadpisać separatorInset W rozszerzeniu, lub B) zamiast tego ustawić proxy wyglądu dla layoutMargins. Nic z tego nie będzie. Mimo że separatorInset jest wskazana jako właściwość, próba nadpisania jej jako Właściwości (lub metody) generuje błędy kompilatora. I ustawienie proxy wyglądu dla UITableViewCell ' s layoutMargins (lub, w tym przypadku, również ustawienie wygląd proxy dla UITableView ' s layoutMargins i separatorInset) nie ma wpływu.

 3
Author: Scott Gardner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-23 08:29:57

Po obejrzeniu odpowiedzi na piętrze 3, próbowałem dowiedzieć się, jaki jest związek między ustawieniem separatora między TableView i TableViewCell i zrobiłem pewien test. Oto moje wnioski:

  1. Możemy uznać, że ustawienie separatora komórki na zero musi przesunąć separator w dwóch krokach: pierwszym krokiem jest ustawienie separatora na zero. drugim krokiem jest ustawienie marginlayout komórki na zero.

  2. Ustaw tabelę separatorinset i marginlayout mogą wpływać na separatorinset komórki. Jednak z testu wynika, że separator TableView wydaje się bezużyteczny, marginlayout TableView może wpływać na marginlayout komórki .

  3. Ustawia PreservesSuperviewLayoutMargins = false, może odciąć efekt TableView marginlayout na komórkach.

  4. Jedno z rozwiązań:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
    
        cell.preservesSuperviewLayoutMargins = false
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero
    
        return cell
    }
    
 2
Author: Xin Chen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-29 10:05:50

To jest moje rozwiązanie. Dotyczy to podklasy custom cell, po prostu dodaj je do podklasy.

  1. - (UIEdgeInsets)layoutMargins {    
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
    

2.

self.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);

I jest to wygodne, że możesz dostosować położenie separatora bez pytania projektanta, aby narysował go dla Ciebie..........

 2
Author: Guoyu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-22 19:49:44
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // ... Get the cell
        cell.separatorInset = UIEdgeInsetsMake(0.f, 20.f, 0.f, [UIScreen mainScreen].bounds.size.width - 20);
        // others
        return cell;
}

Dla każdej konkretnej komórki, którą chcesz ukryć separator.

 1
Author: xmurobi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-06 05:39:41

W bardziej zwarty sposób niż najczęściej głosowana odpowiedź...

- (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];
    }

}

 1
Author: subharb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-09 13:55:15

Dodanie tego fragmentu, proste eleganckie w Swift działa u mnie w iOS8:)

    // tableview single line
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.preservesSuperviewLayoutMargins = false
    cell.layoutMargins = UIEdgeInsetsZero
}
 1
Author: ddnl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-02 14:52:44

To działało idealnie dla mnie w iOS 8 i iOS 9.

Dla 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;
    }
 1
Author: jithin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-06 20:21:47