Cards
Cards copied to clipboard
Reusable issue with UITableView
I would like to thank you for the great work. When we tap on the card you are creating an instance of DetailViewController along with Chaildviewcontroller. when we come out of that screen, you are keeping the view instance in the stack. kindly tell me the solution for this. I'm attaching my sample project in the attachments. kindly look into it as well.
Thanks in advance. AppStoreCardView.zip
the detailViewController you provide in 'shouldPresent' get saved in the card instance because otherwise every time the card get opened I should ask for a new detailViewController to be provided. I'm now fixing some minor bug but I'm considering to redesign the framework to accept a delegate asking for a new detailvc for each opening of the card.
That's a great idea. Meanwhile, if you have any sample project on UITableView, Kindly drop here, please. Thanks.
I edited the shouldPresent method as follows
public func shouldPresent( _ contentViewController: UIViewController?, from superVC: UIViewController?, fullscreen: Bool = false) {
if let content = contentViewController {
self.superVC = superVC
self.fullscreenEnabled = fullscreen
}
}
Comment out the detailVC private variable and add a fullscreenEnabled variable:
//fileprivate var detailVC = DetailViewController()
var fullscreenEnabled = false
Create and setup the contentViewController and DetailViewController on the spot instead of storing as a variable
@objc func cardTapped() {
self.delegate?.cardDidTapInside?(card: self)
//Just create the cardContentVC on the spot, just a test View Controller
let cardContentVC = UIViewController()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = "I am a test label"
label.textColor = UIColor.black
cardContentVC.view.addSubview(label)
//Create and setup the detailVC, this happens every time the Card is tapped now
let content = cardContentVC
let detailVC = DetailViewController()
detailVC.transitioningDelegate = self
detailVC.addChildViewController(content)
detailVC.detailView = content.view
detailVC.delegate = self.delegate
detailVC.isFullscreen = fullscreenEnabled
detailVC.transitioningDelegate = self
detailVC.card = self
//Just like normal, we present the detailVC that is now fully configured
if let vc = superVC {
vc.present(detailVC, animated: true, completion: nil)
} else {
resetAnimated()
}
}
Proposed Better Solution
Create a delegate method called contentViewForCard
@objc func cardTapped() {
self.delegate?.cardDidTapInside?(card: self)
let content = self.delegate?.contentViewControllerForCard?(card: self)
let detailVC = DetailViewController()
detailVC.transitioningDelegate = self
detailVC.addChildViewController(content)
detailVC.detailView = content.view
detailVC.delegate = self.delegate
detailVC.isFullscreen = fullscreenEnabled
detailVC.transitioningDelegate = self
detailVC.card = self
if let vc = superVC {
vc.present(detailVC, animated: true, completion: nil)
} else {
resetAnimated()
}
}
That seems a good solution, could you open a PR so I can test it out ? :)
Sure, I'll open one today. I have been using this method and it has been working but the only issue is that I am presenting a Web View (WKWebView) in each Detail View Controller. By utilizing this delegate method, I successfully avoid the Web View somehow becoming stale and not presenting any content like in @53, but it has to reload the page every time the card is reopened which often takes some time. Unfortunately WKWebView does its own caching so not sure how to cache the Web Content and prevent it from reloading each time...