Ios 将对象映射到二维数组中,用于TableView剖面
我想不出比这更好的办法了。我正在将学生对象的所有属性映射到二维数组中。所以我的电视有分区 我也不能使用静态表视图,如果是这样,这个问题就不存在了 所以我的代码在TVC里Ios 将对象映射到二维数组中,用于TableView剖面,ios,uitableview,swift,multidimensional-array,Ios,Uitableview,Swift,Multidimensional Array,我想不出比这更好的办法了。我正在将学生对象的所有属性映射到二维数组中。所以我的电视有分区 我也不能使用静态表视图,如果是这样,这个问题就不存在了 所以我的代码在TVC里 let currentUser = PFUser.currentUser()! as! MyUser var membershipSection:[[String:String]]! var detailsSection:[[String:String]]! var emergancySection:[[String:Stri
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中使用插入式电视那样,在这些VC中也不能使用静态电视。真烦人!有没有更干净的方法
我可以让它更快速吗?这是创建组合数据源的更好方法
谢谢你的建议
我的最终结果——看起来是这样的——一个带有部分的TVC
我不完全确定你在问什么。“combo”用于什么 如果您想以一种更干净的方式打包数据,Swift中的结构很适合这样做。比如:
struct EmergencySection{
var name: String!
var phone: String!
}
//then to instantiate in loadDisplayDataSource
var emergencySection = EmergencySection(name: "", phone: "")
combo.append(emergencySection)
我是这样做的,稍微干净一点
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:""]
]
}
}
试着使用,对于这样的任务来说,它非常棒。好吧,它是完全客观的,但至少你可以快速地看一看。这个怎么样
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
}
}
通过将UITableViewController添加为子视图控制器,可以将其与普通UIViewController中的静态单元格一起使用
parentVC.addChildViewController(childVC)
childVC.view.frame = parentVC.view.bounds
parentVC.view.addSubview(childVC.view)
childVC.didMoveToParentViewController(parentVC)
combo是tableview在其数据源中使用的。但是我同意,一个结构会把事情清理干净。你是否以某种方式使用
核心数据
来获取你的数据?主要问题是创建组合,我将在我的答案中更新我当前使用的内容。这本巨著实在是太有趣了,我见过各种各样的方法,但没有这种方法。一定要在某个阶段尝试。尝试了此操作,但无法在情节提要中布局静态单元格?如果将UITableView
添加到UIViewController
,则无法添加静态单元格。我的解决方案是用静态单元格制作一个UITableViewController
,它在情节提要中工作,然后作为子视图控制器嵌入UIViewController
。