让我先回答你的问题。
我必须为每个单元格编写一个自己的类吗?=> 是的,我相信是的。至少,我会那样做。
我可以使用一个 tableviewController 吗?=> 是的,你可以。但是,您也可以在视图控制器中拥有一个表格视图。
如何在不同的单元格中填充数据? => 根据条件,您可以在不同的单元格中填充数据。例如,假设您希望前两行类似于第一种类型的单元格。因此,您只需创建/重用第一种类型的单元格并设置它的数据。我想当我给你看屏幕截图时会更清楚。
让我举个例子,在 ViewController 中使用 TableView。一旦你理解了主要概念,你就可以尝试任何你想要的修改。
第 1 步:创建 3 个自定义 TableViewCell。我将它命名为 FirstCustomTableViewCell、SecondCustomTableViewCell、ThirdCustomTableViewCell。您应该使用更有意义的名称。
第 2 步:转到 Main.storyboard 并将 TableView 拖放到您的视图控制器中。现在,选择表格视图并转到身份检查器。将“Prototype Cells”设置为 3。在这里,您刚刚告诉 TableView 您可能有 3 种不同类型的单元格。
第 3 步:
现在,在您的 TableView 和身份检查器中选择第一个单元格,将“FirstCustomTableViewCell”放在自定义类字段中,然后在属性检查器中将标识符设置为“firstCustomCell”。
对所有其他人做同样的事情——将他们的自定义类分别设置为“SecondCustomTableViewCell”和“ThirdCustomTableViewCell”。还将标识符连续设置为 secondCustomCell 和 thirdCustomCell。
第 4 步:编辑自定义单元类并根据需要添加插座。我根据您的问题对其进行了编辑。
P.S:您需要将插座放在类定义下。
所以,在 FirstCustomTableViewCell.swift 的
下
class FirstCustomTableViewCell: UITableViewCell {
你可以把你的标签和图像视图放在出口处。
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myLabel: UILabel!
并在 SecondCustomTableViewCell.swift 中添加两个标签,如-
import UIKit
class SecondCustomTableViewCell: UITableViewCell {
@IBOutlet weak var myLabel_1: UILabel!
@IBOutlet weak var myLabel_2: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
ThirdCustomTableViewCell.swift 应该看起来像-
import UIKit
class ThirdCustomTableViewCell: UITableViewCell {
@IBOutlet weak var dayPicker: UIDatePicker!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
第 5 步:在您的 ViewController 中,为您的 TableView 创建一个 Outlet 并设置来自情节提要的连接。此外,您需要在类定义中添加 UITableViewDelegate 和 UITableViewDataSource 作为协议列表。
所以,你的类定义应该看起来像-
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
之后,将表格视图的 UITableViewDelegate 和 UITableViewDatasource 附加到控制器。此时您的 viewController.swift 应该看起来像 -
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
P.S: 如果你在 ViewController 中使用 TableViewController 而不是 TableView,你可以跳过这一步。
第 6 步:根据 Cell 类拖放单元格中的图像视图和标签。然后从情节提要提供到他们的插座的连接。
第 7 步:现在,在视图控制器中编写 UITableViewDatasource 所需的方法。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "firstCustomCell")
//set the data here
return cell
}
else if indexPath.row == 1 {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCustomCell")
//set the data here
return cell
}
else {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "thirdCustomCell")
//set the data here
return cell
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}