MichiganLabs / AnimatingTableViewHeader

A Swift project explaining how to animate a header above a UITableView
164 stars 35 forks source link

Header Bouncing when table reload #7

Closed mahesh789 closed 5 years ago

mahesh789 commented 5 years ago

Hello John,

I found one issue in UITableview . By default you disabled estimate automatic in uitableview property in storyboard. But in my project that option was enabled. when i will move to other screen and whne i am coming back to previous screen that time i am reloading tableview in viewwillappear. that time my header is bouncing when scrolling time.

Note: Before pushing you just scroll at bottom. then navigate to other screen. when you are coming back to auto header screen and scroll tableview that time you will come to know issue. Thanks.

johndelong commented 5 years ago

Hi @mahesh789 ,

I have a couple questions for you that might help to identify what is going on in your situation :)

  1. Could you clarify what you mean by "estimate automatic"? Sounds like you are saying that it's disabled in my project but enabled in yours - but I'm not exactly sure what property you are referring to. Maybe a screenshot would help?

  2. When you reload your table view, are you inserting/deleting cells? Do you change the scroll position when you reload?

Also, as a side note, viewWillAppear may not be a great place for calling reloadData. I suppose it depends on your situation, but viewDidAppear may help a little in this specific scenario.

mahesh789 commented 5 years ago

Please check your project in attachment file. AnimatingTableViewHeader-master.zip

johndelong commented 5 years ago

Thank you for the example project. I think i have a better idea of what is going on now.

You choose to size the cells using UITableViewAutomaticDimension (which maps to these checkboxes here) image.

This in and of itself is not problematic, but I noticed that you also added the delegate method tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) and specified a hard coded height of 100. As a result, as the table view tries to size itself according to the UITableViewAutomaticDimension rules, it gives the cell only as much room (height wise) as it thinks the cell needs (in our case about 44 points). However, when it finally comes time to show the cell on the screen, your delegate method says, "No wait, I actually want the cell height to be 100 points!" As a result, the table view needs to resize itself and the scrollView.contentSize.height changes.

This cause our math in the scrollViewDidScroll(_ scrollView: UIScrollView) function to get messed up - specifically with the scrollDiff variable. Because the height of the scroll view changed, the diff becomes inflated since the difference from the previous scroll position to the current scroll position is larger (about 56 points larger! 56 + 44 = 100). Finally, as a result, we see the header becoming "bouncy" as we scroll up and down the page.

Although I wouldn't recommend using UITableViewAutomaticDimension AND setting the cell height to a specific value, you have pointed out an issue that is fixable in this example project. I'll update the code shortly and link back here the solution.

johndelong commented 5 years ago

@mahesh789, I believe i've addressed your issue with the above commit. Specifically this line.

Let me know if you have any questions or if this didn't actually solve your issue.

Thanks!

mahesh789 commented 5 years ago

Thank you john i will try this code. I will let you know once issue is resolved.

Thanks.

On Mon, 26 Aug 2019 at 7:15 AM, John DeLong notifications@github.com wrote:

@mahesh789 https://github.com/mahesh789, I believe i've addressed your issue with the above commit. Specifically this line https://github.com/MichiganLabs/AnimatingTableViewHeader/blob/f1f7fc8245d348ff4ed1930610af5cd2aabb8086/Example/ViewController.swift#L73 .

Let me know if you have any questions or if this didn't actually solve your issue.

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/MichiganLabs/AnimatingTableViewHeader/issues/7?email_source=notifications&email_token=AEKMABP6D5NDSX2I3AKTSKLQGMYULA5CNFSM4IO4GNJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5DBGNI#issuecomment-524686133, or mute the thread https://github.com/notifications/unsubscribe-auth/AEKMABOQIGCYFLZN3LHWANTQGMYULANCNFSM4IO4GNJA .

mahesh789 commented 5 years ago

It’s working john than you so much...

On Mon, 26 Aug 2019 at 9:41 AM, Mahesh Kondamuri maheshiphonedev@gmail.com wrote:

Thank you john i will try this code. I will let you know once issue is resolved.

Thanks.

On Mon, 26 Aug 2019 at 7:15 AM, John DeLong notifications@github.com wrote:

@mahesh789 https://github.com/mahesh789, I believe i've addressed your issue with the above commit. Specifically this line https://github.com/MichiganLabs/AnimatingTableViewHeader/blob/f1f7fc8245d348ff4ed1930610af5cd2aabb8086/Example/ViewController.swift#L73 .

Let me know if you have any questions or if this didn't actually solve your issue.

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/MichiganLabs/AnimatingTableViewHeader/issues/7?email_source=notifications&email_token=AEKMABP6D5NDSX2I3AKTSKLQGMYULA5CNFSM4IO4GNJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5DBGNI#issuecomment-524686133, or mute the thread https://github.com/notifications/unsubscribe-auth/AEKMABOQIGCYFLZN3LHWANTQGMYULANCNFSM4IO4GNJA .

mahesh789 commented 5 years ago

It’s working john thank you so much...

On Mon, 26 Aug 2019 at 10:38 PM, Mahesh Kondamuri maheshiphonedev@gmail.com wrote:

It’s working john than you so much...

On Mon, 26 Aug 2019 at 9:41 AM, Mahesh Kondamuri < maheshiphonedev@gmail.com> wrote:

Thank you john i will try this code. I will let you know once issue is resolved.

Thanks.

On Mon, 26 Aug 2019 at 7:15 AM, John DeLong notifications@github.com wrote:

@mahesh789 https://github.com/mahesh789, I believe i've addressed your issue with the above commit. Specifically this line https://github.com/MichiganLabs/AnimatingTableViewHeader/blob/f1f7fc8245d348ff4ed1930610af5cd2aabb8086/Example/ViewController.swift#L73 .

Let me know if you have any questions or if this didn't actually solve your issue.

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/MichiganLabs/AnimatingTableViewHeader/issues/7?email_source=notifications&email_token=AEKMABP6D5NDSX2I3AKTSKLQGMYULA5CNFSM4IO4GNJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5DBGNI#issuecomment-524686133, or mute the thread https://github.com/notifications/unsubscribe-auth/AEKMABOQIGCYFLZN3LHWANTQGMYULANCNFSM4IO4GNJA .

Khanasif07 commented 1 year ago

if you reload only rows instead of complete section, issue will be resolved. Just call this function with proper params like section old count and new count.

extension UITableView{

func reloadRowsInSection(section: Int, oldCount:Int, newCount: Int){

    let maxCount = max(oldCount, newCount)
    let minCount = min(oldCount, newCount)

    var changed = [IndexPath]()

    for i in minCount..<maxCount {
        let indexPath = IndexPath(row: i, section: section)
        changed.append(indexPath)
    }

    var reload = [IndexPath]()
    for i in 0..<minCount{
        let indexPath = IndexPath(row: i, section: section)
        reload.append(indexPath)
    }

    beginUpdates()
    if(newCount > oldCount){
        insertRows(at: changed, with: .fade)
    }else if(oldCount > newCount){
        deleteRows(at: changed, with: .fade)
    }
    if(newCount > oldCount || newCount == oldCount){
        insertRows(at: reload, with: .none)
    }
    endUpdates()
}

}