我希望能够在一个tableview中看到两个不同的单元格。这不是分成几个部分。那么你会把它作为一节还是什么?
答案 0 :(得分:1)
是的,您可以使用例如:
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
}
答案 1 :(得分:0)
UITableViewCell
需要一个标识符,您可以使用它来单独引用它们。
UITableView
可以有多个具有不同标识符的默认单元格。
在Interface Builder上,选择UITableView
,然后在属性检查器上更改Prototype Cells
值以符合您的需要。
然后,选择单元格并为其分配唯一标识符。
如果您使用代码创建UITableView
,则必须为每个唯一标识符注册UITableViewCell
子类:
tableView.registerClass(MyTableViewCell.self, forCellWithReuseIdentifier: "MyCell")
tableView.registerClass(AnotherTableViewCell.self, forCellWithReuseIdentifier: "MyCell")
无论您如何注册单元格,您都可以决定在UITableView
委托方法中使用哪个原型单元
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell