【发布时间】:2018-09-28 11:43:26
【问题描述】:
当我打开开关ON时,我无法从单元格中获取标签。我确实从 Firebase 数据库中获取所有标签。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tagCell", for: indexPath) as! TagsTableViewCell
print(myCallList[indexPath.row])
let _tag = myCallList[indexPath.row]
cell.tagLabel?.text = _tag.type
return cell
}
更新: UITableViewCell 没有什么特别的
import UIKit
class TagsTableViewCell: UITableViewCell {
@IBOutlet weak var tagLabel: UILabel!
@IBOutlet weak var tagSwitch: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我的模特:
class Calls: NSObject {
var type: String?
init(type: String?) {
self.type = type
}
}
LoadCalls 包含 Firebase 数据提取:
func LoadCalls() {
ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
self.myCallList.removeAll()
ref.child("tags").observe(.childAdded, with: { (snapshot) in
if snapshot != nil{
var tagType = snapshot.key as? String
let myCalls = Calls(type: tagType)
self.myCallList.append(myCalls)
print(self.myCallList.count)
DispatchQueue.main.async {
self.tagsTableView.reloadData()
}
}
})
}
【问题讨论】:
-
您希望表格视图控制器在开关更改时获得带有单元格标签值的调用?
-
myCallList的类型是什么?您能否添加TagsTableViewCell类的代码 -
@Carpsen90 我更新了代码.. myCallList 是 NSObject。
-
@Augie 正确...我想在开关打开时获取 label.text。例如,如果 switch_2 为 ON,我想获取 label_2.text
标签: swift switch-statement tableview