2016-06-03 13 views
11

Mam do czynienia z problemem w mojej aplikacji - możesz publikować i edytować swoje ulubione miejsca. Po opublikowaniu wpisu lub edytowaniu określonego wpisu (UITableViewCell) ponownie ładuje się UITableview.UITableview Przewija do góry przy ponownym ładowaniu

Moim problemem jest: UITableview przewija się do góry po ponownym załadowaniu. Ale tego nie chcę. Chcę, aby mój widok pozostał w komórce/widoku, gdzie byłam. Ale nie wiem, jak sobie z tym poradzić.

Czy możesz mi pomóc?

+1

Szukały na metodzie UITableView: reloadRowsAtIndexPaths – Dominic

+0

przyjrzeć się tej odpowiedzi http://stackoverflow.com/questions/28043632/ automatyczne przewijanie do komórki z konkretną wartością, co może być konieczne, aby zapisać indeks, który przeglądasz po przeładowaniu, aby wiedzieć, gdzie przewinąć do –

Odpowiedz

7

MetodareloadData() jest jawnie przeładowaniem siły całego tableView. Działa to dobrze, ale zwykle ma nieprzyjemny wygląd i kiepski interfejs użytkownika, jeśli zamierzasz to zrobić za pomocą widoku tabeli, który aktualnie przegląda użytkownik.

Zamiast tego spójrz na reloadRowsAtIndexPaths(_:withRowAnimation:) i reloadSections(_:withRowAnimation:)in the documentation.

12

Aby zapobiec przewijanie do góry, należy zapisać wyżyny komórkach gdy ładuje i podać dokładną wartość w tableView:estimatedHeightForRowAtIndexPath:

// declare cellHeightsDictionary 
NSMutableDictionary *cellHeightsDictionary; 

// initialize it in ViewDidLoad or other place 
cellHeightsDictionary = @{}.mutableCopy; 

// save height 
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath]; 
} 

// give exact height value 
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSNumber *height = [cellHeightsDictionary objectForKey:indexPath]; 
    if (height) return height.doubleValue; 
    return UITableViewAutomaticDimension; 
} 
14

odpowiedź Igora jest poprawna, jeśli używasz komórki dynamicznie skalowalne (UITableViewAutomaticDimension)

Tutaj jest szybkim 3:

private var cellHeights: [IndexPath: CGFloat?] = [:] 
    var expandedIndexPaths: [IndexPath] = [] 

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     cellHeights[indexPath] = cell.frame.height 
    } 

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
     if let height = cellHeights[indexPath] { 
      return height ?? UITableViewAutomaticDimension 
     } 
     return UITableViewAutomaticDimension 
    } 


    func expandCell(cell: UITableViewCell) { 
     if let indexPath = tableView.indexPath(for: cell) { 
     if !expandedIndexPaths.contains(indexPath) { 
      expandedIndexPaths.append(indexPath) 
      cellHeights[indexPath] = nil 
      tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
      //tableView.scrollToRow(at: indexPath, at: .top, animated: true) 
     } 
     } 
    } 
0

Swift 3,1

DispatchQueue.main.async(execute: { 

    self.TableView.reloadData() 
    self.TableView.contentOffset = .zero 

}) 
+0

, czego nie potrzebujesz Dispatch.main.async dla tego .. –

+0

@OsamaBinBashir nie do końca, ale musisz wywołać tę metodę w głównym wątku. –

+0

@EduardoIrias się z tym zgadzają :) –

0

Wystarczy przejść do tych linii, jeśli chcesz proste rozwiązanie

let contentOffset = tableView.contentOffset 
    tableView.reloadData() 
    tableView.setContentOffset(contentOffset, animated: false) 
Powiązane problemy