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

时间:2015-04-19 11:04:40

标签: ios uitableview swift multidimensional-array

我无法找到更好的方法。我将学生对象的所有属性映射到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 - 创建我的组合数据源的更好方法。

感谢您的任何建议。

我的最终结果 - 工作看起来像这样 - 带有部分的TVC。

enter image description here

5 个答案:

答案 0 :(得分:1)

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

如果您想以更干净的方式打包数据,Swift中的结构对此非常有用。类似的东西:

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

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

答案 1 :(得分:0)

以下是我的表现,更清洁

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

答案 2 :(得分:0)

尝试使用Wikipedia has an excellent in depth explanation.,这对于此类任务非常棒。好吧,它完全是Objective-C,但至少你可以快速看一下它。

答案 3 :(得分:0)

这个怎么样?

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)

通过将UITableViewController添加为普通的UIViewController,可以将其添加为子视图控制器。

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