Comments (17)
and second question, if I have a fixed data,I mean for example for 5 items, can I have an infinite logic?
becuase as I understood infinite example is really relays on infinite data, but can we achive same logic for none infinite data?
from parchment.
Yeah, turns out there was a bug with the way I registered the size delegate. Should be fixed in #135. Will have a new release out shortly ✌️
As for the infinite behavior you’re describing it is kind of possible. Items in Parchment have to be unique so you can’t rely solely on the title, but you can create a data source that subtracts or adds to an index and take the modulo of that index to get the title. Here's an example using the DelegateExample
target:
extension ViewController: PagingViewControllerInfiniteDataSource {
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemAfterPagingItem pagingItem: T) -> T? {
let item = pagingItem as! PagingIndexItem
let index = item.index + 1
let title = cities[abs(index % cities.count)]
return PagingIndexItem(index: index, title: title) as? T
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemBeforePagingItem pagingItem: T) -> T? {
let item = pagingItem as! PagingIndexItem
let index = item.index - 1
let title = cities[abs(index % cities.count)]
return PagingIndexItem(index: index, title: title) as? T
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForPagingItem pagingItem: T) -> UIViewController {
let item = pagingItem as! PagingIndexItem
return CityViewController(title: item.title)
}
}
This will create the illusion of scrolling infinitely with just those items. The problem with this approach is that it will not update the selected cell indicator as you scroll in the header, as each item is unique. I’m not sure how to best add support for this. Either we can add a specific option for a «looping scroll mode», or support having multiple items with the same paging item. I need to get back to you on that.
from parchment.
Released v1.0.3 with the fix now!
from parchment.
First of all thanks for help, this is what I need.
in my case I dont have a scroll option in header, so its fully ok for me ) but I guess it would be nice to have that option.
Thanks a lot
from parchment.
Awesome! I'll create another issue for the looping scrolling 👍
from parchment.
Thanks, also I would suggest in this type of implementation to have an option to use already created controllers in list, I mean
func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForPagingItem pagingItem: T) -> UIViewController {
let item = pagingItem as! PagingIndexItem
return controllers[item.index]
}
to not create each view controller every time
Problem I faced here, that I dont have real index of the item, item.index can be everything, even -1,-2...
Thanks
from parchment.
You could use the same approach as above where you take the modulo of the index:
return controllers[abs(item.index % controllers.count)]
from parchment.
Yea I have tried it, but its not working properly
func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForPagingItem pagingItem: T) -> UIViewController {
let item = pagingItem as! PagingIndexItem
let index = item.index
print(abs(index % cities.count))
return controllers[abs(index % cities.count)]
}
in this case when I am swiping to right(to view previus one) its shows me white empty view controller, and my collectionView dataSource also shows wrong title, I wonder, do we have any logic that each PagingIndexItem should have unique ViewController?
in our example if I have a this list of titles
fileprivate let titles = [
"SHOWS",
"CHATS",
"COMICS",
"PLAYLISTS",
"CONTINUE"
]
should I select it like this in viewDidLoad
pagingViewController.select(pagingItem: PagingIndexItem(index: 0, title: "SHOWS"))
I dont get this select logic, should I set this index to be 0, or something like
pagingViewController.select(pagingItem: PagingIndexItem(index: titles.count, title: "SHOWS"))
Thanks.
from parchment.
The white empty view controller sounds like you are trying to navigate to the same view controller that you are currently on. I'm not sure exactly what's causing this for you, but here is an example of how you could achieve this:
class ViewController: UIViewController {
let viewControllers: [UIViewController] = [
CityViewController(title: "Oslo"),
CityViewController(title: "Shanghai"),
CityViewController(title: "Mumbai"),
CityViewController(title: "Amsterdam"),
CityViewController(title: "Sydney"),
]
override func viewDidLoad() {
super.viewDidLoad()
let pagingViewController = PagingViewController<PagingIndexItem>()
pagingViewController.infiniteDataSource = self
addChildViewController(pagingViewController)
view.addSubview(pagingViewController.view)
view.constrainToEdges(pagingViewController.view)
pagingViewController.didMove(toParentViewController: self)
let item = PagingIndexItem(index: 0, title: viewControllers[0].title ?? "")
pagingViewController.select(pagingItem: item)
}
}
extension ViewController: PagingViewControllerInfiniteDataSource {
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemAfterPagingItem pagingItem: T) -> T? {
let item = pagingItem as! PagingIndexItem
let index = item.index + 1
let title = viewControllers[abs(index % viewControllers.count)].title ?? ""
return PagingIndexItem(index: index, title: title) as? T
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemBeforePagingItem pagingItem: T) -> T? {
let item = pagingItem as! PagingIndexItem
let index = item.index - 1
let title = viewControllers[abs(index % viewControllers.count)].title ?? ""
return PagingIndexItem(index: index, title: title) as? T
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForPagingItem pagingItem: T) -> UIViewController {
let item = pagingItem as! PagingIndexItem
return viewControllers[abs(item.index % viewControllers.count)]
}
}
Here I've replaced the titles array with just an array of view controllers and then use the title
property for the menu items. Does that work for you?
from parchment.
Unfortunately no, if you try this code in that example, and change CityViewController view background color for example to black, and swipe to right, you will see that bug when it shows white screens.
I dont know how to upload recorded video here
from parchment.
Oh okay. I’ll take look and see if I can find a solution 👍
from parchment.
Its correctly working when you are swiping to left(viewing next items), and its starting to work correctly if we swipe right few items(I guess after 5 or 6 items) but when you are swiping from first item previus 6 items it has this bug
from parchment.
ok Thanks a lot
from parchment.
I just find the way to fix this, very hacky way, just putted first selected item index to be Int.max/2 ))
the problem is with -index 0 and +index,when I put selected item to be Int.max/2 I just dont have that problem with -index))
from parchment.
Just wanted to know,if in this case will I have increasing memory usage? or its reuse already created viewcontrollers and cells?
from parchment.
Ah yeah that makes sense. There shouldn’t be any increased memory usage of that approach, so that sounds like a good solution for now. The only thing you need to think about is how many view controllers you are allocating up-front by storing it in an array. But it shouldn’t be an issue with the number of view controllers you’re having.
I’ll explore some ways of making this easier in the future 👍
from parchment.
great, thanks a lot
from parchment.
Related Issues (20)
- PageView does not work in ScrollView HOT 2
- Focus position on left side HOT 1
- Issue on Index HOT 1
- Can i make the indicator fit the text length?
- There is an issue that accidentally gets stuck and cannot keep clicking other tab item HOT 2
- How To Lazy Load Page HOT 1
- How can I hide the menu? (SwiftUI)
- SwiftUI Scrolling glitch HOT 2
- Feature request: SF Icons instead of title text HOT 1
- My app crash when tap to page item. HOT 4
- The package product 'Parchment' cannot be used as a dependency of this target because it uses unsafe build flags. HOT 3
- Lazy loading VC HOT 1
- keyboard avoidance issue HOT 1
- how to make collectionview rounded and add shadow to collectionview HOT 1
- Improve notification list pull to refresh
- outdated code error in PagingSizeCache HOT 3
- Getting Fatal error while using storyboard more than 2 . Unable to reload 3rd storyboard table view HOT 1
- please support indicator fixed width HOT 2
- Position of tableview is reset after tab change in header above menu example HOT 1
- Using UIAlertController in per Item HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parchment.