将对象映射到用于TableView部分的2D数组Swift

我找不出一个更好的方法来做到这一点。 我将学生对象的所有属性映射到一个2D数组中。 所以我的电视有部分。

我也不能使用静态表格视图,如果是的话,这个问题就不存在了。

所以我的代码在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中调用的

 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中使用电视下降,你不能在这些使用静态电视。 这是烦人的! 有更清洁的方法吗?

我可以做更多的SWIFTY – 一个更好的方式来创build我的组合数据源。

感谢您的任何build议。

我的最终结果 – 正在工作看起来像这样 – 与部分TVC。

在这里输入图像说明

我不完全确定你在问什么。 什么是“组合”用于?

如果你想以更简洁的方式打包你的数据,Swift中的结构对此很好。 就像是:

 struct EmergencySection{ var name: String! var phone: String! } //then to instantiate in loadDisplayDataSource var emergencySection = EmergencySection(name: "", phone: "") combo.append(emergencySection) 

尝试使用RETableViewManager ,这对于这样的任务来说非常棒。 那么,它完全是Objective-C,但至less你可以快速浏览它。

这个怎么样?

 import UIKit class ViewController: UITableViewController { var combo = [ [ String: AnyObject? ] ]() let titlesForSection = ["MEMBERSHIP", "DETAILS", "EMERGANCY CONTACT", "MEDICAL HISTORY"] override func viewDidLoad() { super.viewDidLoad() // Something about studen data combo = [ [ "Sessions":"student.sessionsRemaining", "Details":"", "ID":"idString" ] , [ "First Name":"student.firstName", "Last Name":"student.lastName", "DOB":"", "Address":"", "Phone":"", "Email":"student.email", "Occupation":"" ] , [ "Name":"", "Phone":"" ] , [ "Recent Surgery":"", "Hypertension":"", "Diabetes":"", "Caradic":"", "Epilesy":"", "Syncope":"", "Medications":"", "Medical Details":"", "Other Injuries":"" ] ] } override func numberOfSectionsInTableView(tableView: UITableView ) -> Int { return combo.count } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int ) -> Int { return combo[ section ].count } override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return titlesForSection[ section ] } override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell { let v = tableView.dequeueReusableCellWithIdentifier( "SomeIdentifier" ) as! UITableViewCell let w = combo[ indexPath.section ] let wKey = Array( w.keys )[ indexPath.row ] v.textLabel!.text = wKey v.detailTextLabel!.text = w[ wKey ] as? String return v } } 

这是我如何做,有点干净

 private struct Details { static let title = "DETAILS" static let firstName = (key:"First Name", index:0) static let lastName = (key:"Last Name", index:1) static let dob = (key:"DOB", index:2) static let address = (key:"Address", index:3) static let phone = (key:"Phone", index:4) static let email = (key:"Email", index:5) static let occupation = (key:"Occupation", index:6) static func splitIntoDictionaries(student: Student) -> [[String:String]] { return [ [firstName.key:student.firstName], // 0 [lastName.key:student.lastName], // 1 [dob.key:""], [address.key:""], [phone.key:""], [email.key:student.email], [occupation.key:""] ] } } 

你可以在一个普通的UIViewController中使用带有静态单元的UITableViewController,把它作为一个子视图控制器来添加。

 parentVC.addChildViewController(childVC) childVC.view.frame = parentVC.view.bounds parentVC.view.addSubview(childVC.view) childVC.didMoveToParentViewController(parentVC)