如何在tableview中使用带库的结构(How to use a struct with a library in a tableview)

编程入门 行业动态 更新时间:2024-10-27 02:20:23
如何在tableview中使用带库的结构(How to use a struct with a library in a tableview)

我已经创建了一个带有库的结构,并在另一个结构中初始化了该结构。 现在我想在表视图中使用结果结构。 但是,结构当前不能正常工作。 我试图找出原因,但似乎被卡住了。

看起来问题是库没有正确地转换为结构。 例如,当我对结构的实例进行计数时,使用以下代码:

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.count

It 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: 5

You 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 // 5

A 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

更多推荐

本文发布于:2023-08-07 17:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构   如何在   library   tableview   struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!