我需要在 TableView 中设置部分。每个部分都有不同的值类型,请参阅我的代码。但这不起作用。请告知在各部分中设置值的正确方法。请参阅屏幕截图以获取错误消息。注意:我删除了 vardetailsInSection 末尾的“)”,因为它无法正确显示。
var sectionTitles = ["WebSite", "Date Saved", "Document Used","Add Notes"]
var detailsInSection = [([String], [NSDate],[AnyObject],[String])]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return detailsInSection[section].count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DetailsCell")
cell?.textLabel!.text = detailsInSection[indexPath.section][indexPath.row]
return cell!
}
最佳答案
var detailsInSection = [([String], [NSDate],[AnyObject],[String])]
上面是一个元组数组。我从你的代码的其余部分猜测你想要一个数组的数组。
var detailsInSection = [[String](), [NSDate](), [AnyObject](), [String]()]
您的数组将有 4 个元素,每个元素都是一个数组。
如果您想处理不同的类型,请使用 switch 语句。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DetailsCell")!
switch indexPath.section {
case 0:
cell.textLabel!.text = detailsInSection[indexPath.section][indexPath.row]
case 1:
let date = detailsInSection[indexPath.section][indexPath.row]
// Do some date formatting etc
cell.textLabel!.text = PUT YOUR FORMATTED DATE HERE
case 2:
// Do any type casting etc.
let object = detailsInSection[indexPath.section][indexPath.row]
// Convert object to whatever is needed in text format.
cell.textLabel!.text = PUT YOUR OUTPUT HERE
case 3:
// This is a string so just assign it.
cell.textLabel!.text = detailsInSection[indexPath.section][indexPath.row]
default:
// What to do here? Should never happen.
}
return cell
}
关于ios - 我的代码不适用于 TableView 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36748059/