2015-03-31 12 views
17

Dodałem pasek wyszukiwania do mojego widoku tabeli, a gdy użytkownik wyszukuje, chcę, aby widok tabeli przewijał tekst wpisany przez ciebie.Jak automatycznie przewijać widok tabeli? (Swift)

Jak przewinąć widok przewijania automatycznie?

To jest to, co próbowałem, ale pojawia się błąd informujący, że liczba całkowita nie jest wymienialna na ścieżkę indeksu. Co powinienem zrobić?

self.tableview.scrollToRowAtIndexPath(1, atScrollPosition: UITableViewScrollPosition.Middle, animated: true) 

lub

self.tableview.scrollToNearestSelectedRowAtScrollPosition(scrollPosition: UITableViewScrollPosition.Middle, animated: true) 

Odpowiedz

26

Trzeba wywołać metodę scrollToRowAtIndexPath w następujący sposób:

var indexPath = NSIndexPath(forRow: numberOfRowYouWant, inSection: 0) 
self.tableview.scrollToRowAtIndexPath(indexPath, 
       atScrollPosition: UITableViewScrollPosition.Middle, animated: true) 

Powyższy przykład zakłada, że ​​mają tylko jeden punkt. Mam nadzieję, że ci to pomoże.

Dla Swift 3:

let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2) 
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.middle, animated: true) 
+1

kiedy miałaś pasek wyszukiwania, po prostu normalnym tabela: nazywają go w viewDidAppear() i masz ładne animacje, viewDidload pracował także dla mnie, ale bez animacji – kuzdu

+0

jego praca jest w porządku, dziękuję za odpowiednią odpowiedź. – Gaurav

5

Jeśli chcesz przewijać automatycznie:

numberOfSections = tableView.numberOfSections() 
let numberOfRows = tableView.numberOfRowsInSection(numberOfSections-1) 

var indexPath = NSIndexPath(forRow: numberOfRows, inSection: numberOfSections) 
self.tableview.scrollToRowAtIndexPath(indexPath, 
       atScrollPosition: UITableViewScrollPosition.Middle, animated: true) 
+0

Chociaż kod jest doceniany, powinien zawsze mieć towarzyszące wyjaśnienie. To nie musi długo potrwać, ale oczekuje się. – peterh

+0

Dzięki za udostępnienie kodu! Nawiązałem do niego odnośnik, aby utworzyć zaktualizowaną wersję Swift 2.0. – xke

+0

Dzięki, pomóżcie mi bardzo! – javimuu

3

Oto wersja Swift 2.0, aby przewinąć do ostatniego rzędu kilku sekcjach, na podstawie kodu z Thiago wcześniej w tym wątku:

let numberOfSections = self.tableView.numberOfSections 
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1) 

print ("scrolling to bottom row \(numberOfRows)") 

let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: numberOfSections-1) 
self.tableView.scrollToRowAtIndexPath(indexPath, 
         atScrollPosition: UITableViewScrollPosition.Middle, animated: true) 
9

Dla Swift 3 (Xcode 8.1):

 let numberOfSections = self.tableView.numberOfSections 
     let numberOfRows = self.tableView.numberOfRows(inSection: numberOfSections-1) 

     let indexPath = IndexPath(row: numberOfRows-1 , section: numberOfSections-1) 
     self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true) 
0

SWIFT 4

/* Variables */ 
    var scrollTimer = Timer() 


// AN AUTOMATIC SCROLL OF THE YOUT IMAGE 
scrollTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(automaticScroll), userInfo: nil, repeats: true) 




// MARK: - SCROLLVIEW DELEGATE: SHOW CURRENT PAGE IN THE PAGE CONTROL 
func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let pageWidth = containerScrollView.frame.size.width 
    let page = Int(floor((containerScrollView.contentOffset.x * 2 + pageWidth)/(pageWidth * 2))) 
    pageControl.currentPage = page 

} 

// MARK: - AUTOMATIC SCROLL 
@objc func automaticScroll() { 
    var scroll = containerScrollView.contentOffset.x 
    if scroll < CGFloat(view.frame.size.width) * CGFloat(numberOfImages-1) { 
     scroll += CGFloat(view.frame.size.width) 
     containerScrollView.setContentOffset(CGPoint(x: scroll, y:0), animated: true) 
    } else { 
     // Stop the timer 
     scrollTimer.invalidate() 
    } 
} 
-1

Swift 4:

let indexPath = IndexPath(row: numberOfRowYouWant, section: 0) 

self.verbsTable.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true) 
+0

Powinieneś sformatować swój kod. – Nikolaus