Created
November 29, 2020 21:51
-
-
Save christianselig/9622d0e4bf6dd1802cad220e799ac2b2 to your computer and use it in GitHub Desktop.
Trying to figure out how to insert a UITableViewCell above while maintaining scroll position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
let tableView = UITableView(frame: CGRect.zero, style: .plain) | |
var totalSubtracted = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") | |
tableView.dataSource = self | |
tableView.delegate = self | |
view.addSubview(tableView) | |
view.addConstraint(NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0.0)) | |
view.addConstraint(NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0.0)) | |
view.addConstraint(NSLayoutConstraint(item: tableView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0.0)) | |
view.addConstraint(NSLayoutConstraint(item: tableView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 0.0)) | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return UILocalizedIndexedCollation.current().sectionTitles.count + 1 | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
if section == 0 { | |
return totalSubtracted | |
} else if section == 5 { | |
return 15 - totalSubtracted | |
} else { | |
return 8 | |
} | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) | |
cell.textLabel?.text = "Oranges \(indexPath.row)" | |
cell.accessoryView = createStarAccessoryButton() | |
return cell | |
} | |
func sectionIndexTitles(for tableView: UITableView) -> [String]? { | |
return ["*"] + UILocalizedIndexedCollation.current().sectionTitles | |
} | |
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
if section == 0 { | |
return "Favorites" | |
} else { | |
return section == 5 ? "E (Use This One)" : UILocalizedIndexedCollation.current().sectionTitles[section - 1] | |
} | |
} | |
func createStarAccessoryButton() -> UIButton { | |
let button = UIButton(type: .system) | |
button.tintColor = .lightGray | |
button.setImage(UIImage(systemName: "star.fill")!, for: .normal) | |
button.sizeToFit() | |
button.addTarget(self, action: #selector(starButtonTapped(sender:)), for: .touchUpInside) | |
return button | |
} | |
@objc func starButtonTapped(sender: UIButton) { | |
let buttonPosition = sender.convert(CGPoint.zero, to: tableView) | |
guard let indexPath = tableView.indexPathForRow(at: buttonPosition) else { return } | |
let height = tableView.cellForRow(at: indexPath)!.bounds.height | |
let oldContentOffset = tableView.contentOffset.y | |
totalSubtracted += 1 | |
tableView.reloadData() | |
tableView.contentOffset.y = oldContentOffset + height | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment