【发布时间】:2015-09-23 01:07:17
【问题描述】:
我有 2 个部分,第一个有 1 行,第二个有 4 行。我可以在故事板中看到开关、标签和文本字段。它在模拟器中加载空白。我检查了所有的 IBOutlets 都已连接。
以下是我在 Table View Controller 中的代码
import UIKit
class InfoTableViewController: UITableViewController {
var sectionArray: NSMutableArray = ["Social Security Benefits", "Demographics"]
var socialArray: NSMutableArray = ["Estimate Social Security Benefits"]
var demoArray: NSMutableArray = ["Name:", "DOB:", "Age of Retirement:", "Years of Retirement"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return self.sectionArray.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0{
return socialArray.count
} else {
return demoArray.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
println(indexPath.section)
println(indexPath.row)
let cell: SocialTableCell = SocialTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "social")
cell.social?.text = "Estimate"
cell.toggle?.setOn(false, animated: true)
return cell
}
} else if indexPath.section == 1 {
if indexPath.row == 0 {
println(indexPath.section)
println(indexPath.row)
let cell: NameTableCell = NameTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "name")
return cell
}else if indexPath.row == 1{
println(indexPath.section)
println(indexPath.row)
let cell: DOBTableCell = DOBTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "DOB")
}else if indexPath.row == 2{
println(indexPath.section)
println(indexPath.row)
let cell: RetirementTableCell = RetirementTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "AgeOfRetirement")
}else if indexPath.row == 3{
println(indexPath.section)
println(indexPath.row)
let cell: YearsRetirementTableCell = YearsRetirementTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "YearsOfRetirement")
}
}
return UITableViewCell()
//cell.configureFlatCellWithColor(UIColor.greenSeaColor(), selectedColor: UIColor.cloudsColor(), roundingCorners: UIRectCorner())
}
}
我还为每个单元格创建了 5 个表格视图单元格类。我还检查了所有标识符,它们是正确的。
下面是我的故事板图片
但是当我运行它时,模拟器只显示部分并且单元格是空的。
任何帮助将不胜感激。 谢谢
【问题讨论】:
-
你设置了委托和数据源吗?
-
我用的是tableviewcontroller而不是tableview的view controller,它是自动设置的!
-
您需要阅读“表格视图编程指南”并了解如何正确实现您的
cellForRowAtIndexPath方法。您没有正确地将单元格出列,并且没有尝试将数据放入单元格中。这就是为什么它们是空白的。 -
我知道我没有设置较低的单元格,但我设置了社交单元格并且它不起作用。当第一个不工作时,我不想浪费时间设置那些。
-
确保
cell.social和cell.toggle不是nil。
标签: ios swift uitableview uilabel