Skip to content

Instantly share code, notes, and snippets.

@mrfarukturgut
Created August 9, 2021 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrfarukturgut/db8ba518088440b2b61362f0a1d21d28 to your computer and use it in GitHub Desktop.
Save mrfarukturgut/db8ba518088440b2b61362f0a1d21d28 to your computer and use it in GitHub Desktop.
ScrollToBottomTest
import UIKit
extension UITableView {
func scrollToBottom(){
DispatchQueue.main.async {
let indexPath = IndexPath(
row: self.numberOfRows(inSection: self.numberOfSections-1) - 1,
section: self.numberOfSections - 1)
if self.hasRowAtIndexPath(indexPath: indexPath) {
self.scrollToRow(at: indexPath, at: .bottom, animated: false)
}
}
}
func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
}
}
class ViewController: UITableViewController {
private var data: [String] = .init()
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorStyle = .none
tableView.backgroundColor = .systemGroupedBackground
tableView.contentInset = UIEdgeInsets(top: 6, left: 0, bottom: 6, right: 0)
tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now()+1) {
let message = "This is an another test message. "
for i in 1...7 {
self.data.append(String(repeating: message, count: i))
}
self.tableView.reloadData()
self.tableView.scrollToBottom()
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 1000
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "\(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.label.text = data[indexPath.section]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
UITableView.automaticDimension
}
}
class TableViewCell: UITableViewCell {
lazy var label: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
return label
}()
lazy var stackView : UIStackView = {
let view = UIStackView()
view.translatesAutoresizingMaskIntoConstraints = false
view.axis = .vertical
return view
}()
lazy var bubbleView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 4
view.layer.masksToBounds = true
view.backgroundColor = .systemGreen
return view
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
backgroundColor = .clear
contentView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 6).isActive = true
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 12).isActive = true
stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -12).isActive = true
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6).isActive = true
bubbleView.addSubview(label)
label.topAnchor.constraint(equalTo: bubbleView.topAnchor, constant: 12).isActive = true
label.leadingAnchor.constraint(equalTo: bubbleView.leadingAnchor, constant: 12).isActive = true
label.trailingAnchor.constraint(equalTo: bubbleView.trailingAnchor, constant: -12).isActive = true
label.bottomAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: -12).isActive = true
stackView.addArrangedSubview(bubbleView)
}
required init?(coder: NSCoder) {
fatalError()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment