将对象映射到用于TableView部分的二维数组Swift

时间:2021-04-15 21:34:43

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.

我想不出更好的办法来做这件事。我将Student对象的所有属性映射到一个2D数组中。所以我的电视有分段。

I cannot use a Static Tableview either, if so this problem would not exist.

我也不能使用静态Tableview,如果是,这个问题就不存在了。

So my code in the TVC

我在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

//从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?

是否有更好的方法将对象的数据映射到节中?我的方法是可行的,但是有点让人困惑。如果我可以使用静态视图这将会更容易,但是我不能像在普通的VC中使用一个drop in TV那样,你不能在这些中使用静态TV。这是烦人的!有更干净的方法吗?

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.

我的最终结果——它是这样工作的——一个带分段的TVC。

将对象映射到用于TableView部分的二维数组Swift

5 个解决方案

#1


1  

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

我不太清楚你在问什么。“combo”是什么意思?

If you want to just package up your data in a cleaner fashion, structs in Swift are nice for this. Something like:

如果你想要以一种更干净的方式打包你的数据,Swift的结构体就是一个不错的选择。喜欢的东西:

struct EmergencySection{
    var name: String!
    var phone: String!
}

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

#2


0  

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.

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

#3


0  

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

#4


0  

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

#5


0  

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

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

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

#1


1  

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

我不太清楚你在问什么。“combo”是什么意思?

If you want to just package up your data in a cleaner fashion, structs in Swift are nice for this. Something like:

如果你想要以一种更干净的方式打包你的数据,Swift的结构体就是一个不错的选择。喜欢的东西:

struct EmergencySection{
    var name: String!
    var phone: String!
}

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

#2


0  

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.

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

#3


0  

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

#4


0  

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

#5


0  

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

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

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