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

When the PopupViewContreller dismissed,the UITableViewController which presented it,ran reloadData() didn't work #72

Closed
JackTheMico opened this issue Feb 22, 2017 · 10 comments

Comments

@JackTheMico
Copy link

Here's my PopupViewController and how I use it in my UITableViewController.
popup

popintbcontroller

Usually,when DispatchQueue.main.async { self.tableView.reloadData() } worked,the method "cellForRowAtIndexPath" would ran as the nextstep.
But when PopupViewController dismissed,the "reloadData()" ran but "cellForRowAtIndexPath" didn't react, so that the tableView cannot refresh immediately.

@JackTheMico JackTheMico changed the title When the PopupViewContreller dismissed,the UITableViewController which presented it,run reloadData() doesn't work When the PopupViewContreller dismissed,the UITableViewController which presented it,ran reloadData() didn't work Feb 22, 2017
@danlozano
Copy link
Contributor

Hi! Sorry for the late response, have been really busy with work, but I'm taking this whole week to look over all the issues and PR's.

I just want to understand your problem a little bit better.

So it does work when you call

DispatchQueue.main.async { self.tableView.reloadData() }

But not when you call

self.tableView.reloadData()

Or are you trying to say that the table reloading sometimes works and sometimes doesn't?

@JackTheMico
Copy link
Author

Thanks for the response! What I'm trying to say is that after you called
DispatchQueue.main.async { self.tableView.reloadData() }
Then the next two methods which should be called are
overried func tableView(_tableView:UITableView, numberOfRowsInSection section:Int)->Int {}
And
override func tableView(_tableView:UITableView, cellForRowAtIndexPath:IndexPath)->UITableViewCell {}
But these two methods didn't work after you called "reloadData()" with "DispatchQueue,main,async"

@danlozano
Copy link
Contributor

Oh ok.

Well if you're calling reloadData on a table view and those two methods are not being called that means that the releadData is not working.

Is your delegate/data-source everything setup correctly?

Can you share your code in text-form so I can look over it maybe see what's going on.

@danlozano
Copy link
Contributor

There's no reason for calling it on the main queue since the Presentr delegate is called on the main queue.

@JackTheMico
Copy link
Author

MainTableViewController.swift

import Foundation
import UIKit
import Presentr

let DLWUserDefaults = UserDefaults.standard
let SWidth = UIScreen.main.bounds.width
let SHeight = UIScreen.main.bounds.height
let topController = UIApplication.shared.keyWindow?.rootViewController
let mainS = MainTableViewController.shareInstance

class MainTableViewController:UITableViewController{
    
    static let shareInstance = MainTableViewController()
    let MainSB = UIStoryboard.init(name: "Main", bundle: nil)
    let presenter:Presentr = {
        let width = ModalSize.full
        let height = ModalSize.fluid(percentage: 0.4)
        let center = ModalCenterPosition.customOrigin(origin: CGPoint.init(x: 0, y: SHeight/9))
        let presenter = Presentr.init(presentationType: .custom(width: width, height: height, center: center))
        presenter.transitionType = TransitionType.coverVerticalWithSpring
        presenter.dismissTransitionType = TransitionType.coverVerticalFromTop
        return presenter
    }()
    
    lazy  var popupViewController: PopupViewController = {
        let popupViewController:PopupViewController = self.MainSB.instantiateViewController(withIdentifier: "Popup") as! PopupViewController
        // popupViewController.delegate = self
        popupViewController.timeResponse = {(backStr:String)->Void in
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
        return popupViewController
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "baseCell") as! mineCell;
        cell.presenter = self.presenter
        cell.selfController = self
        return cell
    }
}

PopupViewController.swift

import Foundation
import UIKit
import Presentr

typealias TimeReturn = (_ selectTime:String)->Void

class PopupViewController:UIViewController {
    var timeResponse:TimeReturn?
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}

extension PopupViewController:PresentrDelegate {
    func presentrShouldDismiss(keyboardShowing: Bool) -> Bool {
        print("Dismissing View Controller")
        self.timeResponse!("123454")
        return !keyboardShowing
    }
}

mineCell.swift

import Foundation
import UIKit
import Presentr

class mineCell: UITableViewCell {
    var selfController:MainTableViewController?
    var presenter:Presentr?
    @IBOutlet weak var popBtn: UIButton!
    
    
    @IBAction func popBtn_clicked(_ sender: AnyObject) {
        selfController!.customPresentViewController(selfController!.presenter, viewController: mainS.popupViewController, animated: true) {
            
        }
    }
}

Podfile

platform:ios,'8.0'
use_frameworks!
target 'ReusableTest' do
pod 'Presentr'
end

@JackTheMico
Copy link
Author

storyboard1
storyboard2
That's all I got. Sorry about the project's name in 'Podfile',it was a mistake.

@danlozano
Copy link
Contributor

Thanks for all the info!

This looks like it's a problem with your app code, for some reason the table is not reloading.

I would have architected the app a little differently but nothing stands out as me, it should be working, it should be reloading the table.

I will close this issue since this is outside Presentr, BUT if you can/want to submit a zip file with this sample code so I can run it I might help you track down the source of your problem.

@danlozano
Copy link
Contributor

Checked your e-mail, fixed your problem!

  • First I added log messages to each of your 3 table view methods, and realized that you callback was working, and reloadData() was calling number of sections and rows, so that was "working" as well.

  • But later I realized that even though those two methods are being called, the cellForRowAtIndexPath mysteriously was not being called.

Scratched my head a little bit but ultimately realized the problem. You had a static property like so:

static let main = MainTableViewController()

Which honestly is a wrong architecture solution, BUT anyways, the problem is that inside your 'mineCell' you were using the static MainTableViewController, which is a different instance than the one created by you by the interface builder. So you were calling reloadData on a different instance (of the same class) of the view controller, which was calling reloadData, but since there was nothing in view since that view controller is not in view, then cellForRowAtIndexPath is not being called.

Removed the static property, and added a couple of log messages to test everything, and it seems everything is working correctly.

Check your email!

Thanks.

@JackTheMico
Copy link
Author

I finally aware of what I did wrong, I thought the static MainTableViewController and the one created by the
interface builder would be the same one since their addresses are exactly the same one.
But apparently, they are not the same one.
Sorry for bothering you so long , really appreciate your great help!
May happiness and success follow you wherever you go!

@danlozano
Copy link
Contributor

No worries!

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants