【问题标题】:Swift 4 Switch relate to label on tableViewSwift 4 Switch 与 tableView 上的标签相关
【发布时间】: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


【解决方案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
     }
}

【讨论】:

    【解决方案2】:

    我猜是这样的 - 将标签添加到切换器并为其创建操作

    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)
    }
    

    【讨论】:

    • 完美!它有效...只是一点点修复 var getLabel = myCallList[(sender as AnyObject).tag] print(getLabel.type)
    • 这是一个快速的答案和变体,真正更好的方法是按照奥吉通过代表告诉的方式进行
    • 很遗憾我无法将两个答案都标记为正确...但它对您的两个答案都非常有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2018-03-30
    相关资源
    最近更新 更多