Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Object into 2D Array Swift for TableView Sections

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.

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 

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

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?

Can I make this more SWIFTY - A better way to create my combo data source.

Thanks for any advice.

My end result - which is working looks like this - A TVC with sections.

enter image description here

like image 576
DogCoffee Avatar asked Apr 19 '15 11:04

DogCoffee


1 Answers

I'm not entirely sure what you're asking. What is 'combo' used for?

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)
like image 65
Michael Quigley Avatar answered Sep 21 '22 01:09

Michael Quigley