Home How to acces a view from tableViewCell to TableViewController
Reply: 0

How to acces a view from tableViewCell to TableViewController

user4235
1#
user4235 Published in May 27, 2018, 1:00 am

I have a UITableViewController and UITableViewCell. Now I am try to access a view from a UITableViewCell to UITableViewController by didSelectRowAt function. But I could not do it.

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)
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314312 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO