Map object in a 2D array for Swift for TableView sections

I could not find a better way to do this. I map all the properties of the student object in a two-dimensional array. Therefore, my TV has sections.

I cannot use Static Tableview if this problem did not exist.

So my code is in 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 

// 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() } 

Is there a better way to map object data to partitions? The way I do this works, but a little confusing. If I could use a static view, that would be simpler, but I cannot use drop on the TV in Normal VC, and you cannot use static TV in them. What is annoying! Is there a cleaner way?

Can I make it more SWIFTY - the best way to create my combo data source.

Thanks for any advice.

My end result, which works, is as follows: TVC with partitions.

enter image description here

+6
source share
5 answers

I'm not quite sure what you are asking. What is a combo?

If you just want to compose your data in a cleaner way, the structures in Swift are good for this. Sort of:

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

This is how I do it, a little cleaner

 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:""] ] } } 
0
source

Try using the RETableViewManager , which is quite surprising for such tasks. Well, this is completely Objective-C, but at least you could quickly scan it.

0
source

How about this?

 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 } } 
0
source

You can use the UITableViewController with static cells in a regular UIViewController by adding it as a child view controller.

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

Source: https://habr.com/ru/post/985452/


All Articles