xmartlabs / XLPagerTabStrip

Android PagerTabStrip for iOS.
MIT License
6.97k stars 1.33k forks source link

How can reload menus programmatically? #535

Open pingMob opened 6 years ago

pingMob commented 6 years ago

Hello How are you? I tried to used XLPagerTabStrip library. It worked great. When I add new menu programmatically . I received crash issue. I tried to call self.reloadPagerTabStripView() to reload menus. I got crash issue on this part:

    for index in childViewControllers.indices {
        let nElements = childViewControllers.count - index
        let n = (Int(arc4random()) % nElements) + index
        if n != index {
            childViewControllers.swapAt(index, n)
        }
    }
    let nItems = 1 + (arc4random() % UInt32(childViewControllers.count))
    return Array(childViewControllers.prefix(Int(nItems)))

So I removed these lines. and just added

return childViewControllers

And tried to continue. But still got crash issue. //ButtonBarPagerTabStripViewController.swift // MARK: - UICollectionViewDelegateFlowLayut

@objc open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
    guard let cellWidthValue = cachedCellWidths?[indexPath.row] else {
        fatalError("cachedCellWidths for \(indexPath.row) must not be nil")
    }
    return CGSize(width: cellWidthValue, height: collectionView.frame.size.height)
}

seems to be related to cachedCellWidths. crash reason was "Thread 1: Fatal error: Index out of range"

I debugged . cachedCellWidths had 4 values. and indexPath.row was 4. I tried to add or remove menus programmatically. Could you explain me how can i fix issue? Thank you screenshot at feb 05 04-08-29

nickbit commented 6 years ago

Potential fix here: https://github.com/xmartlabs/XLPagerTabStrip/issues/388