我发现用弹出的RSSectionMenu库看here后,用我所选择的单元格的值更新UI按钮的文本,我可以执行以下操作:
selectionMenu.setSelectedItems(items: simpleSelectedArray) { (text, isSelected, selectedItems) in
// update your existing array with updated selected items, so when menu presents second time updated items will be default selected.
self.simpleSelectedArray = selectedItems
}
我尝试在我的代码中执行此操作,但它不起作用。当我打印simpleSelectedArray时它是空的。那么请问我如何收集选定的单元格值并更新我的标题按钮。我是Swift的初学者,谢谢!
这是我如何使用POD包括第一条评论中给出的建议。
@IBAction func CategoryTapped(_ sender: Any) {
let simpleDataArray = cat //I COLLECT cat for category and catID for category ID from my API
let simpleDataArrayID = catId
let selectionMenu = RSSelectionMenu(dataSource: simpleDataArray) { (cell, object, indexPath) in
cell.textLabel?.text = object
cell.tintColor = .orange
}
selectionMenu.setSelectedItems(items: simpleSelectedArray) { (text, isSelected, selectedItems) in
self.simpleSelectedArray.append(contentsOf: selectedItems)
self.simpleSelectedArrayID.append(contentsOf: simpleDataArrayID)
for j in 0 ..< self.simpleSelectedArray.count {
self.categoryupdate = self.simpleSelectedArray[j]
self.EditCategory.setTitle(self.categoryupdate,for: .normal)
self.categoryupdateID = self.simpleSelectedArrayID[j]
}
self.updateAdcat(Category: self.categoryupdateID ) // Here I want to update my category by sending the ID of the selected category to the server. updateAdcat(id: Int) is my update function
}
selectionMenu.show(style: .Push, from: self)
}
现在的问题是我无法更新按钮标题并收集正确的相应ID。所选标题和Id不对应。