ios

[ios - Swift] UITableView 특정 위치로 이동

POKY_0908 2020. 11. 26. 09:54

 

 

 

 

1. scrollToRow

IndexPath의 section과 row의 위치로 이동을 하게 됩니다.

 

아래와 같은 확장 메서드를 만들어 사용해보도록 하겠습니다.

 

 

 

import Foundation
import UIKit

extension UITableView {
    
    func GoToBottom() {
    
        DispatchQueue.main.async {
            let section = self.numberOfSections - 1
            let row = self.numberOfRows(inSection: section) - 1
            
            let indexPath = IndexPath(row: row, section: section)
            self.scrollToRow(at: indexPath, at: .bottom, animated: false)
        }
    }
    
}

 

 

@IBOutlet weak var mTableView: UITableView!

mTableView.GoToBottom()

 

 

 

2. setContentOffset

UITableView ContentSize의 해당하는 위치로 이동합니다. 절대 위치 값인 것 같습니다.

 

 

 mTableView.setContentOffset(CGPoint(x: 0, y: offsetY), animated: false)

기본적인 사용방법이며 상대 위치로 변경하고 싶다면 

 

UITableView의 contentOffset 을 사용하여 Offset값을 가감해 사용하면 됩니다.