【发布时间】:2019-12-17 23:37:34
【问题描述】:
所以我收集视图允许用户选择多个类别来展示他们的照片。当我选择一个单元格时,打印语句可以正常工作,它会按预期打印单元格 childValue 的名称。当我去提交帖子时,它只会将最后选择的索引上传到数据库。
问题的一个示例 - 如果我选择“打印”和“绿色”类别,则照片只会发布“绿色”,因为它是最后选择的索引。如何保存用户所做的所有选择并将帖子发送到选定的 childValues?
var selectedCategoryValue = ""
var trend: Trend!
var style: Style!
// When a cell is selected, the postID will be added to the correct childvalue that is relevent to the post
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == trendCollectionView {
switch selectedSegmentIndex {
case 0: self.trend = femaleTrends[indexPath.row]
print("You selected row \(trend.childValue)")
self.selectedCategoryValue = trend.childValue
case 1: self.trend = maleTrends[indexPath.row]
print("You selected row \(trend.childValue)")
self.selectedCategoryValue = trend.childValue
default: break
}
} else {
// Once I figure out how to store multiple selected values, I will complete else statement
switch selectedSegmentIndex {
case 0: self.style = femaleStyles[indexPath.row]
case 1: self.style = maleStyles[indexPath.row]
default: break
}
}
}
这是按下提交并将照片上传到服务器时使用的功能
func uploadPostToCategory(withPostId postId: String) {
let selectedValue = [postId: 1]
switch trend.gender {
case .female:
// add postId to childValue based on cell selection
FEMALE_TRENDS_REF.child(selectedCategoryValue).updateChildValues(selectedValue)
case.male:
MALE_TRENDS_REF.child(selectedCategoryValue).updateChildValues(selectedValue)
}
}
在此处的集合视图单元格内处理isSelected。
override var isSelected: Bool {
didSet{
if self.isSelected {
trendCheckmark.isHidden = false
}
else {
trendCheckmark.isHidden = true
}
}
}
【问题讨论】:
标签: swift xcode firebase-realtime-database uicollectionview