在分段控件中向表视图添加数据的问题

问题描述 投票:0回答:1

我有一个包含两个项目的分段控件,

// 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]'的值来键入'[ParticipatedStoriesData]'

我似乎无法解决这个问题,希望有人可以帮助我,谢谢! :)

ios swift uitableview uisegmentedcontrol
1个回答
0
投票

您重复相同的模型

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
}
© www.soinside.com 2019 - 2024. All rights reserved.