TableViewController中的多个自定义单元格

时间:2018-08-20 15:41:21

标签: swift uitableview

场景


|第一个单元格|



|第二细胞| |开关| | |



|第三单元 | TextField |


我创建了具有三个不同类的动态表视图:SecondTableCell(内部有开关的出口)和ThirdTableCell(内部有textField的出口)

开关处于关闭状态。

我需要看到textField.isUserInteractionEnabled = false,然后开关才打开。

我该怎么做?

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var customTableView: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellSwitch = tableView.dequeueReusableCell(withIdentifier: "cellSwitch")! as! SwitchTableViewCell
        let cellText = tableView.dequeueReusableCell(withIdentifier: "cellText")! as! TextFieldTableViewCell

}

class SwitchTableViewCell: UITableViewCell {

    @IBOutlet weak var switchOutlet: UISwitch!

    @IBAction func switchAction(_ sender: UISwitch) {
        if sender.isOn == true{
            print("swithOn")
        }else{
            print("swithOff")
        }
    }

}


class TextFieldTableViewCell: UITableViewCell {

    @IBOutlet weak var textFieldColor: UITextField!
}

2 个答案:

答案 0 :(得分:0)

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwitchDelegate {

    @IBOutlet weak var customTableView: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cellSwitch = tableView.dequeueReusableCell(withIdentifier: "cellSwitch")! as! SwitchTableViewCell
        let cellText = tableView.dequeueReusableCell(withIdentifier: "cellText")! as! TextFieldTableViewCell

        cellSwitch.delegate = self
        cellText.delegate = self
    }

    func valueDidChange(isOn: Bool) {
        tableView.visibleCells.forEach { cell
            if let cell = cell as? TextFieldTableViewCell {
                cell.textField.isUserInteractionEnabled = false
            }
        }
    }

}

protocol SwitchDelegate {
    func valueDidChange(isOn: Bool)
}

class SwitchTableViewCell: UITableViewCell {

    @IBOutlet weak var switchOutlet: UISwitch!
    var delegate: SwitchDelegate?

    @IBAction func switchAction(_ sender: UISwitch) {
        delegate?.valueDidChange(isOn: sender.isOn)
    }
} 


class TextFieldTableViewCell: UITableViewCell {
     @IBOutlet weak var textField: UITextField!
}

答案 1 :(得分:0)

在tableviewcontroller中设置单元格时,可以在视图行中定义要使用哪个tableviewcell类。就像下面的示例一样:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if ((indexPath.row < 25 && counter > 25) || (indexPath.row < counter)) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SaleItems", for: indexPath) as! MyTableCellTableViewCell
        let TapRecognize = UITapGestureRecognizer(target: self, action: #selector(self.PictureTap(sender:)))
        TapRecognize.numberOfTapsRequired = 1
        TapRecognize.name = jSonData[indexPath.row]["advid"]

        // Configure the cell...
        cell.Megnevezes.text=jSonData[indexPath.row]["advdescription"]!
        cell.EladasiAr.text=jSonData[indexPath.row]["advprice"]! + " " + jSonData[indexPath.row]["advpricecur"]!
        cell.addGestureRecognizer(TapRecognize)
        cell.LoadPicture(AdvID: Int(jSonData[indexPath.row]["advid"]!)!)
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PagerCell", for: indexPath) as! ChangePageCell
        cell.delegate = self
        return cell
    }

}

在我的示例中,表视图的最后一行是不同的。从您的解释中还不清楚,您真的想对开关单元做什么。但是,您应该确保可以识别单元格及其类,以知道将操作传递到哪里。

在我的情况下,gestureRecognizer会获得一个名称,该名称包含一个用于加载照片的相关广告ID,并链接到该广告详细信息。

在单元格上点击时,它会调用点击识别器的动作,并且名称可以用来识别被触摸的单元格。