Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace DelegateProxy with concrete forward implementation #58

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions ReverseExtension/ReverseExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ extension UITableView {
let base = nonNilBase
let section = max(0, max(0, (base.numberOfSections - 1)) - indexPath.section)
let numberOfRowsInSection = base.numberOfRows(inSection: reversed ? section : indexPath.section)
let row = max(0, numberOfRowsInSection - 1 - indexPath.row)
let row = reversed ? max(0, numberOfRowsInSection - 1 - indexPath.row) : indexPath.row
return IndexPath(row: row, section: section)
}

Expand Down Expand Up @@ -412,6 +412,8 @@ extension UITableView.ReverseExtension: UITableViewDelegate {
cell.contentView.transform = CGAffineTransform.identity.rotated(by: .pi)
UIView.setAnimationsEnabled(true)
}

delegate?.tableView?(tableView, willDisplay: cell, forRowAt: reversedIndexPath(with: indexPath))
}

public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
Expand All @@ -420,6 +422,8 @@ extension UITableView.ReverseExtension: UITableViewDelegate {
view.transform = CGAffineTransform.identity.rotated(by: .pi)
UIView.setAnimationsEnabled(true)
}

delegate?.tableView?(tableView, willDisplayHeaderView: view, forSection: reversedSection(with: section))
}

public func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
Expand All @@ -428,6 +432,30 @@ extension UITableView.ReverseExtension: UITableViewDelegate {
view.transform = CGAffineTransform.identity.rotated(by: .pi)
UIView.setAnimationsEnabled(true)
}

delegate?.tableView?(tableView, willDisplayFooterView: view, forSection: reversedSection(with: section))
}

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
delegate?.tableView?(tableView, heightForHeaderInSection: reversedSection(with: section)) ?? .leastNonzeroMagnitude
}

public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
delegate?.tableView?(tableView, heightForFooterInSection: reversedSection(with: section)) ?? .leastNonzeroMagnitude
}

public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
delegate?.tableView?(tableView, editingStyleForRowAt: indexPath) ?? .delete
}

@available(iOS 11.0, *)
public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
delegate?.tableView?(tableView, trailingSwipeActionsConfigurationForRowAt: indexPath)
}

@available(iOS 11.0, *)
public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
delegate?.tableView?(tableView, leadingSwipeActionsConfigurationForRowAt: indexPath)
}
}

Expand Down Expand Up @@ -456,7 +484,19 @@ extension UITableView.ReverseExtension: UITableViewDataSource {
public func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return dataSource?.tableView?(tableView, titleForHeaderInSection: reversedSection(with: section))
}


public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return delegate?.tableView?(tableView, viewForHeaderInSection: reversedSection(with: section))
}

public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return delegate?.tableView?(tableView, viewForFooterInSection: reversedSection(with: section))
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.tableView?(tableView, didSelectRowAt: reversedIndexPath(with: indexPath))
}

// Editing

// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ struct MessageModel {
let imageName: String
let message: String
let time: String

}

extension MessageModel {

init() {
imageName = Const.imageNames[Int(arc4random_uniform(UInt32(Const.imageNames.count)))]
message = Const.messags[Int(arc4random_uniform(UInt32(Const.messags.count)))]
Expand Down
29 changes: 25 additions & 4 deletions ReverseExtensionSample/ReverseExtensionSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

fileprivate var messages: [MessageModel] = []
private var sections: [[MessageModel]] = (1...10)
.map { i -> [MessageModel] in
(0..<i)
.map {
MessageModel(imageName: "marty1", message: "\($0) hello", time: "00:00")
Copy link
Contributor Author

@toshi0383 toshi0383 Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marty-suzuki
You can change MessageModel contents after merge, but please keep

  • displaying indexPath to screen
  • and to have different number of rows for each section

so it better demonstrates how the things work under the hood.

}
}

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -39,28 +45,43 @@ class ViewController: UIViewController {
}

@IBAction func addButtonTapped(_ sender: UIBarButtonItem) {
let i = sections.count - 1
var messages = sections[i]
messages.append(MessageModel())
sections[i] = messages
tableView.beginUpdates()
tableView.re.insertRows(at: [IndexPath(row: messages.count - 1, section: 0)], with: .automatic)
tableView.re.insertRows(at: [IndexPath(row: messages.count - 1, section: i)], with: .automatic)
tableView.endUpdates()
}

@IBAction func trashButtonTapped(_ sender: UIBarButtonItem) {
messages.removeAll()
sections.removeLast(max(0, sections.count - 1))
tableView.reloadData()
}
}

extension ViewController: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
sections[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let messages = sections[indexPath.section]
(cell as? TableViewCell)?.configure(with: messages[indexPath.row])
return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let messages = sections[indexPath.section]
let message = messages[indexPath.row]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this PR, you likely to get crash here because given indexPath is not "reversed", which does not match indexPath passed to cellForRow.

print("willDisplay: \(message) at (section: \(indexPath.section), row: \(indexPath.row))")
}
}


Expand Down