ios - 如何从 tableViewCell 访问 View 到 TableViewController

标签 ios swift uitableview

我有一个 UITableViewControllerUITableViewCell。现在我尝试通过 didSelectRowAt 函数访问从 UITableViewCellUITableViewController 的 View 。但我做不到。

表格 View Controller

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

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

最佳答案

试试这个:

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

编辑2 在上面的解决方案中替换此方法

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

希望对你有用

关于ios - 如何从 tableViewCell 访问 View 到 TableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48780532/

相关文章:

xcode - 如何在 Swift 中修改方法中的 Dictionary 对象?

ios - Pickerview不显示

ios - Tableview 剪切了最后一行但返回后正确

ios - 应用程序关闭时如何在iOS应用程序中分页

ios - 当我使用 Storyboard id 时如何将数据传递到目标 View ?

swift - 在 collectionView Swift 中加载数据时显示事件指示器

swift - UITableView ContentInset 和 ContentOffset

iphone - 可滚动且与 UITable 滚动同步的背景图像

ios - NavigationLink 与 SwiftUI : Type of expression is ambiguous without more context

objective-c - UITableViewCell 子类在所有 super 方法上都出现构建错误