Parchment icon indicating copy to clipboard operation
Parchment copied to clipboard

after reloadMenu the first menu item is always selected

Open minaMagedNaeem opened this issue 4 years ago • 7 comments

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

minaMagedNaeem avatar Jun 15 '20 01:06 minaMagedNaeem

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

minaMagedNaeem avatar Jun 15 '20 03:06 minaMagedNaeem

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 avatar Jul 05 '20 09:07 rechsteiner

@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.

alasmanis avatar Jul 16 '20 10:07 alasmanis

any progress here? I met this problem too!

MonipichMP avatar Feb 08 '21 01:02 MonipichMP

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

HuuLiang avatar Mar 05 '21 10:03 HuuLiang

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 avatar Aug 30 '21 23:08 davidhsu1115

@davidhsu1115 you need to register the custom class for the given item type. See the docs here: https://github.com/rechsteiner/Parchment#custom-cells

rechsteiner avatar Sep 18 '21 14:09 rechsteiner