如何从tableViewCell访问视图到TableViewController

时间:2018-02-14 05:55:28

标签: ios swift uitableview

我有UITableViewControllerUITableViewCell。现在,我尝试通过UITableViewCell函数访问UITableViewControllerdidSelectRowAt的视图。但我做不到。

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

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

4 个答案:

答案 0 :(得分:1)

试试这个:

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

希望它对您有用

答案 1 :(得分:0)

这样做

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

答案 2 :(得分:0)

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

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

代码应如下所示:

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

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

答案 3 :(得分:0)

<强> TableViewController

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

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