![](/img/trans.png)
[英]Swift : Switching tableview cells in cellForRowAtIndexPath
[英]What should I write in tableView(_:cellForRowAtIndexPath:) method to custom cells?
我正在設計一個帶有3個自定義單元格的tableview。 我想通過添加唯一的附件類型來自定義它們。 我用靜態單元格創建了一個tableViewController。 我為每個單元格編寫了一個特殊的標識符和特殊的類。
3個自定義單元格的tableView基金應如何顯示?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CityCellIdentifier = "CityTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(CityCellIdentifier, forIndexPath: indexPath) as! CityTableViewCell
// Configure the cell...
return cell
}
我應該為此創建三個單元格變量嗎?
根據我在這里的回答,使用您自己的以下代碼變體:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
if (criteria for cell 1) {
let cell = tableView!.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as? cell1
return (cell)
}
else {
let cell = tableView!.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as? cell2
return (cell)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.