当我打开开关时,我无法从电池中获取标签。我确实从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
}
}
LoadCall包含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()
}
}
})
}
答案 0 :(得分:1)
在单元格和表控制器之间进行通信的委托/协议可以在这里很好地工作。
protocol switchCellDelegate : Class {
func cellSwitchChanged( value: String, sender: Any)
}
使用属性和IBAction更新表视图单元以进行开关更改
class TagsTableViewCell: UITableViewCell {
weak var delegate : switchCellDelegate?
@IBAction func switchChanged(sender: UISwitch){
guard let delegate = delegate else { return }
if sender.isOn {
delegate.cellSwitchChanged( value: tagLabel.text, sender: self)
}
}
然后在cellForRowAtIndex中添加此
cell.delegate = self
和控制器
extension myController : switchCellDelegate {
func cellSwitchChanged( value: String, sender: Any){
//do what you want here
}
}
答案 1 :(得分:0)
我猜是这样-将标签添加到切换台并为其创建操作
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
cell.switcher.tag = indexPath.row
return cell
}
在此之后
@IBAction func switcherChanged(_ sender: UISwitch) {
var getLabel = myCallList[(sender as AnyObject).tag]
print(getLabel.type)
}