Parchment
Parchment copied to clipboard
after reloadMenu the first menu item is always selected
I have a paging view controller that contains two child view controllers, each view controller will update its menu item text so i have the new text stored in an array of strings of length 2, when i update the strings, i call reloadMenu to refresh menu items labels, the issue that it always selects the first menu item without even switching to the first view controller, just the bottom selection line of the menu items gets moved to the first one and that's it, I tried calling select on the last index but it didn't work
To furthur explain, I am trying to change menu items label text in runtime by specifying new strings and then calling reloadMenu which then selects the first menu item losing sync ex: i am standing in the second view controller and after calling reloadMenu, selected menu item is the first
Hi @minaMagedNaeem! When calling reloadMenu it keeps the current selection as long as the selected PagingItem does not become a new item. For instance, if you are using PagingIndexItem
it uses the title property to determine the uniqueness of the item. This means that if you change the title of that item, it counts as a new completely new item and the selection is reset to the first item. If you want to keep the same selection while change the title property you need to implement your own PagingItem
where the title is not part of the Hashable
@rechsteiner could you please give more info on washable. e.g. in IconItem (from example), I'm using hasher like:
func hash(into hasher: inout Hasher) { hasher.combine(index) }
but it still reloads the entire menu (on reloadMenu()). I'm trying to add a badge to each item that (atm using tier to update badge info and reloadMenu()), but as above - sets menu 1st item as selected but doesn't change the active VC.
Also weird thing: I'm having 3 menu items, only 2nd's badge gets updated; if 1st is selected - all is fine; if last (3rd) is selected - its also all ok; problem persists only if the 2nd item (with badge updating) is selected. Thx!
p.s. IconItems are stored in the property [] so they are not being recreated as in the example
p.p.s. atm as a workaround I'm using a class object (private var badgeInfo: BadgeInfo) as a property for IconItem struct to service the badge info.
any progress here? I met this problem too!
struct CSRecordItem:PagingItem{
public let index: Int
public var title: String
public init(index: Int, title: String) {
self.index = index
self.title = title
}
func isEqual(to item: PagingItem) -> Bool {
guard let recordItem = item as? CSRecordItem else {
return false
}
return self.index == recordItem.index
}
func isBefore(item: PagingItem) -> Bool {
guard let recordItem = item as? CSRecordItem else {
return false
}
return self.index < recordItem.index
}
var identifier: Int {
return index
}
}
just implement PagingItem protocol with index ,ignore title
struct CSRecordItem:PagingItem{
public let index: Int public var title: String public init(index: Int, title: String) { self.index = index self.title = title } func isEqual(to item: PagingItem) -> Bool { guard let recordItem = item as? CSRecordItem else { return false } return self.index == recordItem.index } func isBefore(item: PagingItem) -> Bool { guard let recordItem = item as? CSRecordItem else { return false } return self.index < recordItem.index } var identifier: Int { return index }
}
just implement PagingItem protocol with index ,ignore title
Hi, I am trying to use your reply to solve this problem, but app keep crashing due to "'could not dequeue a view of kind: UICollectionElementKindCell with identifier CSRecordItem - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'" Any idea how can i fix it. Thx!
@davidhsu1115 you need to register the custom class for the given item type. See the docs here: https://github.com/rechsteiner/Parchment#custom-cells