如何在UITableView中显示2个自定义的单元格
我必须在表格中显示2个不同的单元格。 我已经尝试了通过设置一个原型到一个表。 但仍然显示Prototype table cells must have reuse identifiers
警告。
有人可以指导我解决这个警告。
跟随这个链接: UITableview与多个自定义单元格与Swift
在故事板中,您必须定义单元格的标识符,如下图所示
然后在cellForRowAtIndexPath
你必须使用特定的标识符这样的特定单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier1") //set the data here return cell } else if indexPath.row == 1 { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier2") //set the data here return cell } }
您必须为这两个原型单元设置Reuse Identifier
,它们必须不同。 然后在你的cellForItemAtIndexPath
方法中,你必须根据给定的indexPath使用相应的Reuse Identifier
来出列单元。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableView { switch indexPath.section { case 0: return tableView.dequeueReusableCellWithIdentifier("CustomCell1", forIndexPath: indexPath) case 1: return tableView.dequeueReusableCellWithIdentifier("CustomCell2", forIndexPath: indexPath) break: return UITableViewCell() } }