ios - 使用两个不同的自定义 UITableView Cells
问题描述
我想要多个单元格,每个单元格都有一个标签。但作为最后一个单元格,我想要一个按钮而不是标签,它应该导航回我的主视图控制器。
标签正在工作,但我无法制作一个带有按钮的单元格运行。这是我的 UITableViewController 代码:
class StatsViewController: UITableViewController {
var vc = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
if let label = cell.viewWithTag(1000)as? UILabel{
if indexPath.row == 0 {
label.text = "Row 0"
}
else if indexPath.row == 1{
label.text = "Row 1"
}
else if indexPath.row == 2{
label.text = "Row 2"
}
else if indexPath.row == 3{
label.text = "Row 3"
}
return cell
}
if let button = cell2.viewWithTag(1001) as? UIButton {
if indexPath.row == 4{
return cell2
}
}
return UITableViewCell()
}
}
我用“cell”标识了情节提要中的标签单元,用“cell2”标识了纽扣单元。
然后我用值“1000”标记标签,并且 cell2 中的按钮有 Tag = 1001
(WEITER 是按钮)
感谢@Sh_Khan 和@ 这样编辑我的代码。现在,它确实向我显示了按钮,但没有显示标签..
class StatsViewController: UITableViewController {
var vc = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
// tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// tableView.reloadData()
//tableView.delegate = self
}
let timestamp = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: .medium, timeStyle: .short)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellId = (indexPath.row == 4) ? "cell2" : "cell"
if let label = cell.viewWithTag(1000)as? UILabel{
if indexPath.row == 0 {
label.text = "Row 0"
}
else if indexPath.row == 1{
label.text = "Row 1"
}
else if indexPath.row == 2{
label.text = "Row 2"
}
else if indexPath.row == 3{
label.text = "Row 3"
}
return tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
}
if let button = cell2.viewWithTag(1001) as? UIButton {
if indexPath.row == 4{
return tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
}
}
return UITableViewCell()
}
}
解决方案
在您当前的代码中
if let button = cell2.viewWithTag(1001) as? UIButton {
永远不会在这里回归
return cell
将永远运行,你需要
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexpath.row == 19) { // last row as you have 20
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
return cell
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if let label = cell.viewWithTag(1000)as? UILabel{
if indexPath.row == 0 {
label.text = "Row 0"
}
else if indexPath.row == 1{
label.text = "Row 1"
}
else if indexPath.row == 2{
label.text = "Row 2"
}
else if indexPath.row == 3{
label.text = "Row 3"
}
return cell
}
return cell
}
}
提示:不要使用标签创建网点
推荐阅读
- scala - 闭包内的对象生命周期
- c - C:[错误]链表快速排序
- c# - C# - 在某些值之间替换文本文件中的字符串
- jboss - jboss-6.0.0.Final 与 wildfly-10.1.0.Final 中的每个用户数据源连接池(通过安全域)
- groovy - Groovy XMLParser & XMLSlurper
- binary-search-tree - 从前序遍历构造 bst
- python-3.x - Homebrew 不会在 /usr/local/bin 上创建 pip3 符号链接
- angularjs - UI-路由器动态“抽象”属性
- java - node.js 上的服务器 + android 客户端 + json 文件
- reactjs - Reactjs 不可变状态到可变