ios - 将对象映射到 TableView 部分的二维数组 Swift

标签 ios uitableview swift multidimensional-array

我想不出更好的方法来做到这一点。我正在将 Student 对象的所有属性映射到二维数组中。所以我的电视有部分。

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

所以我在 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()
}

有没有更好的方法将对象的数据映射到部分?我这样做的方式有效,但有点令人困惑。如果我可以使用静态 View ,这会更容易,但我不能像在普通 VC 中使用电视下降一样,你不能在这些中使用静态电视。这很烦人!有更清洁的方法吗?

我能否使它更快速 - 一种创建组合数据源的更好方法。

感谢您的任何建议。

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

enter image description here

最佳答案

我不完全确定你在问什么。 “组合”有什么用?

如果您只想以更简洁的方式打包数据,Swift 中的结构非常适合。像这样的东西:

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

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

关于ios - 将对象映射到 TableView 部分的二维数组 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728972/

相关文章:

iphone - 如何应用正则表达式和/或清理电话线?

iphone - UITableViewCell - 理解 "reuseable"

ios - 解码不匹配的数据类型时应用程序崩溃 : how to verify type before decoding with NSCoding?

ios - 如何使用 alamofire 实现 put http 请求

ios - 为什么 Swift Firebase auth() 响应晚?

ios - iOS 应用程序中的实时和开发 URL

ios - 没有 Storyboard标识符的 performSegueWithIdentifier

uitableview - 专注于 UISearchBar 但键盘没有出现

ios - 需要帮助将位置数据发送到 Xcode 中的服务器

ios - 如何在 objective-c 中设置推送通知的声音?