我已经创建了一个带有库的结构,并在另一个结构中初始化了该结构。 现在我想在表视图中使用结果结构。 但是,结构当前不能正常工作。 我试图找出原因,但似乎被卡住了。
看起来问题是库没有正确地转换为结构。 例如,当我对结构的实例进行计数时,使用以下代码:
var personalDetailsStructExtra: [PersonalDetailsStruct] = [] personalDetailsStructExtra.count它返回0,而它应该是5(参见下面的代码,字典中有5个条目):
struct PersonalDetailsStructLibrary { let library = [ [ "title": "Country", "icon": "country.pdf", "questions": ["Belgium", "France", "Germany", "Netherlands", "Sweden", "UK", "USA"] ], [ "title": "Age", "icon": "age.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Gender", "icon": "gender.pdf", "questions": ["Male", "Female", "Other"] ], [ "title": "Height", "icon": "height.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Weight", "icon": "weight.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], ] }和
struct PersonalDetailsStruct { var title: String? var icon: UIImage? var questions: [String] = [] init(index: Int) { let personalDetailsStructLibrary = PersonalDetailsStructLibrary().library let personalDetailsDictionary = personalDetailsStructLibrary[index] title = personalDetailsDictionary["title"] as! String! let iconNamePD = personalDetailsDictionary["icon"] as! String! icon = UIImage(named: iconNamePD!) questions += personalDetailsDictionary["artists"] as! [String] } }正如您在代码中看到的那样,我希望使用struct在我的表视图中用UIPickerView(问题)填充标签(标题),图像(图标)和UITextView。
由于它不起作用,我正在寻找:
答:关于如何使这个代码在tableview中工作的反馈
B:我是否应该使用另一种方法来填充tableview中的动态单元格
I have created a struct with a library and have initialised that struct in another struct. Now I want to use the resulting struct in a Table View. However, the struct currently doesn't work properly. I've tried to find out why, but seem to be stuck.
It looks like the issue is that the library doesn't get translated properly to the struct. For in instance, when I do a count on an instance of the struct, using the following code:
var personalDetailsStructExtra: [PersonalDetailsStruct] = [] personalDetailsStructExtra.countIt returns 0, while it should be 5 (See code below, there are 5 entries into the dictionary):
struct PersonalDetailsStructLibrary { let library = [ [ "title": "Country", "icon": "country.pdf", "questions": ["Belgium", "France", "Germany", "Netherlands", "Sweden", "UK", "USA"] ], [ "title": "Age", "icon": "age.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Gender", "icon": "gender.pdf", "questions": ["Male", "Female", "Other"] ], [ "title": "Height", "icon": "height.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Weight", "icon": "weight.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], ] }And
struct PersonalDetailsStruct { var title: String? var icon: UIImage? var questions: [String] = [] init(index: Int) { let personalDetailsStructLibrary = PersonalDetailsStructLibrary().library let personalDetailsDictionary = personalDetailsStructLibrary[index] title = personalDetailsDictionary["title"] as! String! let iconNamePD = personalDetailsDictionary["icon"] as! String! icon = UIImage(named: iconNamePD!) questions += personalDetailsDictionary["artists"] as! [String] } }As you can see in the code I want use the struct to fill up a label (title), image (icon) and UITextView with UIPickerView (questions) in my table view.
Since it doesn't work, I'm looking for either:
A: Feedback on how to make this code work in a tableview
B: Whether I should use another method to populate the dynamic cells in my tableview
最满意答案
您必须初始化personalDetailsStructExtra数组,然后才能看到所需的计数。
var personalDetailsStructExtra = [PersonalDetailsStruct]() PersonalDetailsStructLibrary().library.count let count = PersonalDetailsStructLibrary().library.count for i in 0..<count { personalDetailsStructExtra.append(PersonalDetailsStruct(index: i)) } personalDetailsStructExtra.count // 5更好的选择是使用Library结构来构造和维护所有模型对象。
struct PersonalDetailDataSource { let library = [ [ "title": "Country", "icon": "country.pdf", "questions": ["Belgium", "France", "Germany", "Netherlands", "Sweden", "UK", "USA"] ], [ "title": "Age", "icon": "age.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Gender", "icon": "gender.pdf", "questions": ["Male", "Female", "Other"] ], [ "title": "Height", "icon": "height.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Weight", "icon": "weight.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], ] var personalDetails = [PersonalDetail]() init() { loadData() } mutating private func loadData() { for i in 0..<library.count { let personalDetailsDictionary = library[i] let title = personalDetailsDictionary["title"] as! String! let iconName = personalDetailsDictionary["icon"] as! String! let questions = personalDetailsDictionary["questions"] as! [String] personalDetails.append(PersonalDetail(title: title, iconName: iconName, questions: questions)) } } } struct PersonalDetail { var title: String? var icon: UIImage? var questions: [String] = [] init(title: String, iconName: String, questions: [String]) { self.title = title if let icon = UIImage(named: iconName) { self.icon = icon } self.questions = questions } } PersonalDetailDataSource().personalDetails.count // count: 5You have to initialize the personalDetailsStructExtra array, only then you would see the required count.
var personalDetailsStructExtra = [PersonalDetailsStruct]() PersonalDetailsStructLibrary().library.count let count = PersonalDetailsStructLibrary().library.count for i in 0..<count { personalDetailsStructExtra.append(PersonalDetailsStruct(index: i)) } personalDetailsStructExtra.count // 5A better option is to use the Library struct to construct and maintain all model objects.
struct PersonalDetailDataSource { let library = [ [ "title": "Country", "icon": "country.pdf", "questions": ["Belgium", "France", "Germany", "Netherlands", "Sweden", "UK", "USA"] ], [ "title": "Age", "icon": "age.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Gender", "icon": "gender.pdf", "questions": ["Male", "Female", "Other"] ], [ "title": "Height", "icon": "height.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], [ "title": "Weight", "icon": "weight.pdf", "questions": ["1", "2", "3", "4", "5", "6", "7"] ], ] var personalDetails = [PersonalDetail]() init() { loadData() } mutating private func loadData() { for i in 0..<library.count { let personalDetailsDictionary = library[i] let title = personalDetailsDictionary["title"] as! String! let iconName = personalDetailsDictionary["icon"] as! String! let questions = personalDetailsDictionary["questions"] as! [String] personalDetails.append(PersonalDetail(title: title, iconName: iconName, questions: questions)) } } } struct PersonalDetail { var title: String? var icon: UIImage? var questions: [String] = [] init(title: String, iconName: String, questions: [String]) { self.title = title if let icon = UIImage(named: iconName) { self.icon = icon } self.questions = questions } } PersonalDetailDataSource().personalDetails.count // count: 5更多推荐
发布评论