Автоматическая прокрутка к нижней части UITableView - PullRequest
0 голосов
/ 07 мая 2018

Как можно автоматически прокрутить до конца UIViewController после открытия страницы?

В настоящее время, когда я открываю страницу, она прокручивается полностью до самого верха. Но мне это нужно внизупотому что именно здесь самые последние сообщения

Вот быстрый способ просмотра таблицы:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "messages") as! UITableViewCell        
    cell.selectionStyle = .none
    let message = cell.viewWithTag(100) as! UILabel
    let name = cell.viewWithTag(101) as! UILabel
    let data = self.dataArr[indexPath.row] as! [String:AnyObject]
    message.text = " " + String(describing: data["message"]!) + " "
    name.text = String(describing: data["name"]!)

    return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var mapStr = ""
    let data = self.dataArr[indexPath.row] as! [String:AnyObject]
    let message = String(describing: data["message"]!)
    let array = message.components(separatedBy: "\n") as! [String]
    let arrayLoc = message.components(separatedBy: "Location: ") as! [String]
        if arrayLoc.count > 0 {
            mapStr = arrayLoc[1]
        }
    print(mapStr)
    if mapStr.count > 0 {
        self.open(scheme: mapStr)
    }

}

}

Ответы [ 2 ]

0 голосов
/ 06 октября 2018

Это сработало для меня в Swift 4

Я переопределил функцию viewDidAppear моего класса UITableViewController.

override func viewDidAppear(_ animated: Bool) {
    let scrollPoint = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.frame.size.height)
    self.tableView.setContentOffset(scrollPoint, animated: false)
}

Благодаря этому посту Прокрутите вниз до конца UITableView для ответа, обратите внимание, что вопрос не совсем тот же, они прокручиваются вниз по reloadData, а не по экрану.

0 голосов
/ 07 мая 2018
let indexPath = NSIndexPath(item: noOfRows, section: 0)
yourTableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.bottom, animated: true)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...