【发布时间】:2015-10-06 23:55:27
【问题描述】:
我希望能够在一个表格视图中看到两个不同的单元格。这没有被分成几个部分。那么,您会选择一个部分还是什么?
【问题讨论】:
-
你能实际展示你的尝试吗?
我希望能够在一个表格视图中看到两个不同的单元格。这没有被分成几个部分。那么,您会选择一个部分还是什么?
【问题讨论】:
是的,您可以这样做,例如:
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! FirstTableViewCell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! SecondTableViewCell
default:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell3", forIndexPath: indexPath) as! ThirdTableViewCell
}
【讨论】:
UITableViewCells 需要一个标识符,您可以使用它来单独引用它们。
UITableView 可以有多个具有不同标识符的默认单元格。
在 Interface Builder 上,选择您的 UITableView,然后在 Attributes Inspector 上更改 Prototype Cells 值以满足您的需求。
然后,选择单元格并为其分配一个唯一标识符。
如果您使用代码创建 UITableView,则必须为每个唯一标识符注册一个 UITableViewCell 子类:
tableView.registerClass(MyTableViewCell.self, forCellWithReuseIdentifier: "MyCell")
tableView.registerClass(AnotherTableViewCell.self, forCellWithReuseIdentifier: "MyCell")
无论您如何注册单元格,您都可以决定在UITableViews 委托方法上使用哪个原型单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
【讨论】: