DogCoffee
DogCoffee

Reputation: 19956

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

Upvotes: 6

Views: 1856

Answers (5)

pteofil
pteofil

Reputation: 4163

You can use a UITableViewController with static cells in a normal 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)

Upvotes: 0

DogCoffee
DogCoffee

Reputation: 19956

Here is how I am doing it, a bit 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:""]
        ]
    }
}

Upvotes: 0

Satachito
Satachito

Reputation: 5888

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

Upvotes: 0

ilidar
ilidar

Reputation: 81

Try using RETableViewManager, it's pretty awesome for such tasks. Well, it's fully Objective-C, but at least you could have a quick look of it.

Upvotes: 0

Michael Quigley
Michael Quigley

Reputation: 56

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)

Upvotes: 1

Related Questions