我如何在Swift中的一个UITableViewController中查询两个自定义单元格?

时间:2015-10-06 23:55:27

标签: arrays swift swift2

我希望能够在一个tableview中看到两个不同的单元格。这不是分成几个部分。那么你会把它作为一节还是什么?

2 个答案:

答案 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值以符合您的需要。

enter image description here

然后,选择单元格并为其分配唯一标识符。

enter image description here

如果您使用代码创建UITableView,则必须为每个唯一标识符注册UITableViewCell子类:

tableView.registerClass(MyTableViewCell.self, forCellWithReuseIdentifier: "MyCell")
tableView.registerClass(AnotherTableViewCell.self, forCellWithReuseIdentifier: "MyCell")

无论您如何注册单元格,您都可以决定在UITableView委托方法中使用哪个原型单元

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell