Ios 如何将视图从tableViewCell访问到TableViewController

Ios 如何将视图从tableViewCell访问到TableViewController,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableViewController和UITableViewCell。现在,我正在尝试通过didSelectRowAt函数从UITableViewCell访问视图到UITableViewController。但我做不到 TableViewController import Foundation import UIKit class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{

我有一个
UITableViewController
UITableViewCell
。现在,我正在尝试通过
didSelectRowAt
函数从
UITableViewCell
访问视图到
UITableViewController
。但我做不到


TableViewController

import Foundation
import UIKit

class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
    let flipCellId = "flipCellid"

    let flipTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .green

        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .gray

        flipTableView.delegate = self
        flipTableView.dataSource = self

        flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

        self.view.addSubview(flipTableView)

        flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)
    }

    let countryArray = ["bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
    let cityArray = ["Dhake","Kathmandu",  "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countryArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell

        //cell.textLabel?.text = countryArray[indexPath.row]
        cell.zeroLabel.text = countryArray[indexPath.row]
        cell.oneLabel.text = cityArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        UIView.transition(with: FlipTableViewCell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

        FlipTableViewCell.zeroView.isHidden = true
        FlipTableViewCell.oneView.isHidden = false
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        UIView.transition(with: FlipTableViewCell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

        FlipTableViewCell.zeroView.isHidden = false
        FlipTableViewCell.oneView.isHidden = true
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.flipTableView.frame.width / 4
    }
}
    import Foundation
import UIKit

class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
    let flipCellId = "flipCellid"

    let flipTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .green
        tableView.allowsMultipleSelection = true
        return tableView
    }()


    let countryArray = ["India","bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
    let cityArray = ["Delhi","Dhake","Kathmandu",  "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .gray

        flipTableView.delegate = self
        flipTableView.dataSource = self

        flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

        self.view.addSubview(flipTableView)

        self.resetFlag()

        flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)


    }
    var flagArray : [String] = []

    func resetFlag() {

        flagArray.removeAll(keepingCapacity: true)

        for _ in 0 ..< self.countryArray.count {
            self.flagArray.append("0")
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countryArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell

        cell.selectionStyle = .none

        if flagArray[indexPath.row] == "0" {
            cell.zeroView.isHidden = false
            cell.oneView.isHidden = true
        }else{
            cell.zeroView.isHidden = true
            cell.oneView.isHidden = false
        }

        cell.zeroLabel.text = countryArray[indexPath.row]
        cell.oneLabel.text = cityArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {

            self.flagArray.remove(at: indexPath.row)
            self.flagArray.insert("1", at: indexPath.row)

            UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: { (complete) in
                cell.zeroView.isHidden = true
                cell.oneView.isHidden = false
            })

        }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {

            self.flagArray.remove(at: indexPath.row)
            self.flagArray.insert("0", at: indexPath.row)

            UIView.transition(with: cell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: { (complete) in
                cell.zeroView.isHidden = false
                cell.oneView.isHidden = true
            })

        }

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.flipTableView.frame.width / 4
    }
}
TableViewCell

import UIKit

class FlipTableViewCell: UITableViewCell{
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    static let zeroView = flipView(myColor: .yellow)
    static let oneView = flipView(myColor: .green)

    static func flipView(myColor: UIColor) -> UIView {
        let view = UIView()
        view.backgroundColor = myColor

        return view
    }

    let zeroLabel: UILabel = {
        let lb = UILabel()
        lb.text = "Zero 0"
        return lb
    }()

    let oneLabel: UILabel = {
        let lb = UILabel()
        lb.text = "one 1"
        return lb
    }()

    func setupView(){
        FlipTableViewCell.zeroView.frame = CGRect(x: 0, y: 0, width: frame.width, height:frame.height)
        FlipTableViewCell.oneView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)

        self.addSubview(FlipTableViewCell.zeroView)
        self.addSubview(FlipTableViewCell.oneView)

        zeroLabel.frame = CGRect(x: 20, y: 0, width: self.frame.width - 40, height: 50)
        oneLabel.frame = CGRect(x: 20, y:0, width: self.frame.width - 40, height: 50)

        addSubview(zeroLabel)
        addSubview(oneLabel)
    }
}
    import UIKit

class FlipTableViewCell: UITableViewCell{
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     let zeroView = flipView(myColor: .yellow)
     let oneView = flipView(myColor: .green)

    static func flipView(myColor: UIColor) -> UIView {
        let view = UIView()
        view.backgroundColor = myColor

        return view
    }

    let zeroLabel: UILabel = {
        let lb = UILabel()
        lb.text = "Zero 0"
        return lb
    }()

    let oneLabel: UILabel = {
        let lb = UILabel()
        lb.text = "one 1"
        return lb
    }()

    func setupView(){
        zeroView.frame = CGRect(x: 0, y: 0, width: frame.width, height:frame.width/4)
        oneView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width/4)

        self.addSubview(oneView)
        self.addSubview(zeroView)


        zeroLabel.frame = CGRect(x: 20, y: (frame.width/4)/2 - 25, width: self.frame.width - 40, height: 50)
        oneLabel.frame = CGRect(x: 20, y:(frame.width/4)/2 - 25, width: self.frame.width - 40, height: 50)

        zeroView.addSubview(zeroLabel)
        oneView.addSubview(oneLabel)
    }
}
试试这个:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
        UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

        cell.zeroView.isHidden = true
        cell.oneView.isHidden = false
    }
}
编辑

我已更新您的
FlipTableViewCell

看起来像这样

class FlipTableViewCell: UITableViewCell{
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var zeroView : UIView!
    var oneView : UIView!

    func flipView(myColor: UIColor) -> UIView {
        let view = UIView()
        view.backgroundColor = myColor

        return view
    }

    let zeroLabel: UILabel = {
        let lb = UILabel()
        lb.text = "Zero 0"
        return lb
    }()

    let oneLabel: UILabel = {
        let lb = UILabel()
        lb.text = "one 1"
        return lb
    }()

    func setupView(){
        zeroView = flipView(myColor: .yellow)
        oneView  = flipView(myColor: .green)

        zeroView.frame = CGRect(x: 0, y: 0, width: frame.width, height:frame.height)
        oneView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)

        self.addSubview(zeroView)
        self.addSubview(oneView)

        zeroLabel.frame = CGRect(x: 20, y: 0, width: self.frame.width - 40, height: 50)
        oneLabel.frame = CGRect(x: 20, y:0, width: self.frame.width - 40, height: 50)

        addSubview(zeroLabel)
        addSubview(oneLabel)
    }
}
并在
FlipViewCon

class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
    let flipCellId = "flipCellid"

    let flipTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .green

        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .gray

        flipTableView.delegate = self
        flipTableView.dataSource = self

        flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

        self.view.addSubview(flipTableView)

        flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)
    }

    let countryArray = ["bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
    let cityArray = ["Dhake","Kathmandu",  "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countryArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell

        //cell.textLabel?.text = countryArray[indexPath.row]
        cell.zeroLabel.text = countryArray[indexPath.row]
        cell.oneLabel.text = cityArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
            UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

            cell.zeroView.isHidden = true
            cell.oneView.isHidden = false
        }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
            UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

            cell.zeroView.isHidden = false
            cell.oneView.isHidden = true
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.flipTableView.frame.width / 4
    }
}
Edit2 在上述解决方案中替换此方法

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
            UIView.transition(with: cell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

            cell.zeroView.isHidden = false
            cell.oneView.isHidden = true
        }
    }
希望它对你有用

像这样做吧

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! FlipTableViewCell
    // TODO:
}

就像这篇文章中的所有其他答案一样,使用
UITableView
上的
cellForRow(at:IndexPath)
方法获取表视图中单元格的引用

如果单元格已加载(可见),则此方法返回
UITableViewCell
;如果indexPath不正确或未加载单元格,则返回
nil

代码应该如下所示:

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath)
    // Do something with cell.
}

TableViewController

import Foundation
import UIKit

class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
    let flipCellId = "flipCellid"

    let flipTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .green

        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .gray

        flipTableView.delegate = self
        flipTableView.dataSource = self

        flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

        self.view.addSubview(flipTableView)

        flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)
    }

    let countryArray = ["bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
    let cityArray = ["Dhake","Kathmandu",  "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countryArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell

        //cell.textLabel?.text = countryArray[indexPath.row]
        cell.zeroLabel.text = countryArray[indexPath.row]
        cell.oneLabel.text = cityArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        UIView.transition(with: FlipTableViewCell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

        FlipTableViewCell.zeroView.isHidden = true
        FlipTableViewCell.oneView.isHidden = false
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        UIView.transition(with: FlipTableViewCell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)

        FlipTableViewCell.zeroView.isHidden = false
        FlipTableViewCell.oneView.isHidden = true
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.flipTableView.frame.width / 4
    }
}
    import Foundation
import UIKit

class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
    let flipCellId = "flipCellid"

    let flipTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .green
        tableView.allowsMultipleSelection = true
        return tableView
    }()


    let countryArray = ["India","bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
    let cityArray = ["Delhi","Dhake","Kathmandu",  "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .gray

        flipTableView.delegate = self
        flipTableView.dataSource = self

        flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

        self.view.addSubview(flipTableView)

        self.resetFlag()

        flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)


    }
    var flagArray : [String] = []

    func resetFlag() {

        flagArray.removeAll(keepingCapacity: true)

        for _ in 0 ..< self.countryArray.count {
            self.flagArray.append("0")
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countryArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell

        cell.selectionStyle = .none

        if flagArray[indexPath.row] == "0" {
            cell.zeroView.isHidden = false
            cell.oneView.isHidden = true
        }else{
            cell.zeroView.isHidden = true
            cell.oneView.isHidden = false
        }

        cell.zeroLabel.text = countryArray[indexPath.row]
        cell.oneLabel.text = cityArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {

            self.flagArray.remove(at: indexPath.row)
            self.flagArray.insert("1", at: indexPath.row)

            UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: { (complete) in
                cell.zeroView.isHidden = true
                cell.oneView.isHidden = false
            })

        }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {

            self.flagArray.remove(at: indexPath.row)
            self.flagArray.insert("0", at: indexPath.row)

            UIView.transition(with: cell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: { (complete) in
                cell.zeroView.isHidden = false
                cell.oneView.isHidden = true
            })

        }

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.flipTableView.frame.width / 4
    }
}

使用
tableView.cellForRow(at:IndexPath)
方法,在
didSelectRowAt
@Sam中请访问更多详细信息,朋友!顺便说一句,我不知道如何成功地使用这些静态的zeroView和oneView。对于整个应用程序,每个应用程序只能有一个应用程序,但您试图将它们作为子视图粘贴到每个UITableViewCell中。您真正要做的就是在重新为他们做父母时将他们从一个单元格移动到另一个单元格。@Emma Ani签出我的更新答案当用户滚动时,这将失败。@rmaddy您能帮我吗!我相信你知道如何解决我的问题problem@jigneshVadadoriya它只是在没有文本标签的情况下翻转
yellowView
,下一个greenView根本不会翻转。朋友,请尝试更正,我希望你能这样做。这将失败,因为用户滚动表视图和单元格得到重用。要解决这个问题,我们必须维护带有ex ON/OFFi am agree rmaddy标志的数组列表。但现在我需要helpwait@emmani,我将向您发送已解决的示例。你能为我分享一下用户界面截图吗cell@SomParkash我热切地等着你