【发布时间】:2019-10-15 05:37:18
【问题描述】:
我有一个包含两个项目的分段控件,
// Create a segmented control for the stories
let segmentedControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Participated Stories", "Drafts"])
sc.addTarget(self, action: #selector(handleSegmentChange), for: .valueChanged)
sc.selectedSegmentIndex = 0
return sc
}()
// Create a list to store all the participated stories
let participatedStories = ["Hello", "Hey", "Hi"]
// Create a list to store all the drafts
let drafts = ["Sup", "Whatsup", "Gone"]
// Create a list to store the list that needs to be displayed
lazy var rowsToDisplay = participatedStories
@objc fileprivate func handleSegmentChange() {
switch segmentedControl.selectedSegmentIndex {
case 0:
rowsToDisplay = participatedStories
default:
rowsToDisplay = drafts
}
tableView.reloadData()
}
handleSegmentChange 函数的作用是根据分段控件中选择的选项卡更改表格视图中的数据。这工作正常。现在我想从 Firebase 中检索数据并在表格视图中显示数据,这是我尝试过的:
// The data for the story Drafts
struct DraftStoriesData {
var storyKey: String
var storyTitle: String
var votes: Int
}
// The data for the participated stories
struct ParticipatedStoriesData {
var storyKey: String
var storyTitle: String
var votes: Int
}
let participatedStories: [ParticipatedStoriesData] = []
let drafts: [DraftStoriesData] = []
lazy var rowsToDisplay = participatedStories
@objc fileprivate func handleSegmentChange() {
switch segmentedControl.selectedSegmentIndex {
case 0:
rowsToDisplay = participatedStories
default:
rowsToDisplay = drafts
}
tableView.reloadData()
}
但是这样做会出错 -
无法将类型“[DraftStoriesData]”的值分配给类型 '[参与的故事数据]'
【问题讨论】:
标签: ios swift uitableview uisegmentedcontrol