UITableView+Extension.swift 718 B

12345678910111213141516171819202122232425
  1. //
  2. // UITableView+Extension.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/8.
  6. //
  7. import Foundation
  8. import UIKit
  9. extension UITableView {
  10. func scrollToBottom(animated: Bool = true) {
  11. // 获取最后一组的最后一行索引
  12. let lastSection = numberOfSections - 1
  13. guard lastSection >= 0 else { return } // 没有分组时直接返回
  14. let lastRow = numberOfRows(inSection: lastSection) - 1
  15. guard lastRow >= 0 else { return } // 分组内没有数据时直接返回
  16. // 滚动到最后一行
  17. let indexPath = IndexPath(row: lastRow, section: lastSection)
  18. scrollToRow(at: indexPath, at: .bottom, animated: animated)
  19. }
  20. }