问题描述
我无法找到更好的方法。我将学生对象的所有属性映射到2D数组。所以我的电视有部分。
I could not figure out a better way of doing this. I am mapping all the properties of the Student Object into a 2D Array. So my TV has sections.
我也不能使用静态表视图,如果是这样,这个问题就不存在了。
I cannot use a Static Tableview either, if so this problem would not exist.
所以我在TVC中的代码
So my code in the TVC
let currentUser = PFUser.currentUser()! as! MyUser
var membershipSection:[[String:String]]!
var detailsSection:[[String:String]]!
var emergancySection:[[String:String]]!
var medicalSection:[[String:String]]!
var titlesForSection = ["MEMBERSHIP", "DETAILS", "EMERGANCY CONTACT", "MEDICAL HISTORY"]
var combo = [[[String:String]]]() // Data Source for TableView
//从ViewDidLoad调用以下内容
// The following is called from ViewDidLoad
func loadDisplayDataSource() {
combo.removeAll(keepCapacity: true)
var idString = "Awaiting ID Generation"
if student.objectId != nil {
idString = student.objectId!
}
membershipSection = [["Sessions":student.sessionsRemaining], ["Details":""], ["ID":idString]]
detailsSection = [["First Name":student.firstName], ["Last Name":student.lastName], ["DOB":""], ["Address":""], ["Phone":""], ["Email":student.email], ["Occupation":""]]
emergancySection = [["Name":""], ["Phone":""]]
medicalSection = [["Recent Surgery":""], ["Hypertension":""], ["Diabetes":""], ["Caradic":""], ["Epilesy":""], ["Syncope":""], ["Medications":""], ["Medical Details":""], ["Other Injuries":""]]
combo.append(membershipSection)
combo.append(detailsSection)
combo.append(emergancySection)
combo.append(medicalSection)
self.tableView.beginUpdates()
var range = NSMakeRange(0, self.numberOfSectionsInTableView(self.tableView))
var sections = NSIndexSet(indexesInRange: range)
self.tableView.deleteSections(sections, withRowAnimation: UITableViewRowAnimation.None)
self.tableView.insertSections(sections, withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.endUpdates()
}
有没有更好的方法将对象的数据映射到各个部分?我这样做的方式有效,但有点令人困惑。如果我可以使用静态视图这将更容易,但我不能在普通VC中使用电视放映,你不能在这些中使用静态电视。这很烦人!是否有更清洁的方式?
Is there a better way to map a object's data into sections ? The way I'm doing it works, but is a little confusing. If i could use a static view this would be easier, but I cannot as using a drop in TV within a Normal VC and you cannot use static TV in these. Which is annoying! Is there a cleaner way?
我可以使这更多SWIFTY - 创建我的组合数据源的更好方法。
Can I make this more SWIFTY - A better way to create my combo data source.
感谢您提出任何建议。
我的最终结果 - 正常工作 - 带有部分的TVC。
My end result - which is working looks like this - A TVC with sections.
推荐答案
我不完全确定你在问什么。什么是'combo'用于?
I'm not entirely sure what you're asking. What is 'combo' used for?
如果你想以更干净的方式打包数据,Swift中的结构对此很好。类似于:
If you want to just package up your data in a cleaner fashion, structs in Swift are nice for this. Something like:
struct EmergencySection{
var name: String!
var phone: String!
}
//then to instantiate in loadDisplayDataSource
var emergencySection = EmergencySection(name: "", phone: "")
combo.append(emergencySection)
这篇关于将对象映射到用于TableView节的2D数组Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!