如何在 Swift 的故事板中将两个自定义单元格添加到 tableview 中? [英] How to add two custom cell into tableview in storyboard in Swift?
本文介绍了如何在 Swift 的故事板中将两个自定义单元格添加到 tableview 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有两个不同自定义单元格的 tableView.一个单元格有一个开关,另一个单元格有一个图像,我有两个单独的单元格自定义类,标识符..但我看不到这一点.我不知道我是否需要将故事板中的配置更改为动态或其他内容.. 我可以显示两个不同的自定义单元格
I have a tableView with two different custom cell. One cell have a switch and other a image, I have Two separated custom class for the cells, identifiers.. but I can't see this. i don't know if I need change the configuration in story board to dynamic or other thing.. Can I show two different custom cells
谢谢!
推荐答案
检查显示每个特定自定义单元格的条件,然后根据需要投射到该单元格:
Check the criteria for showing each specific custom cell, then cast to that cell as needed:
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)
}
}
<小时>
Swift 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath -> UITableViewCell {
if (criteria for cell 1) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
return cell
}
}
这篇关于如何在 Swift 的故事板中将两个自定义单元格添加到 tableview 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文