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

How to acces a view from tableViewCell to TableViewController

Emma Ani
1#
Emma Ani Published in 2018-02-14 05:55:28Z

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)
    }
}
jignesh Vadadoriya
2#
jignesh Vadadoriya Reply to 2018-02-14 06:50:15Z

Try this:

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

Edit

I have update your FlipTableViewCell

it looks like this

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

And Change some code in 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 Replace this method in above solution

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

Hope it will work for you

rmaddy
3#
rmaddy Reply to 2018-02-14 06:06:01Z

Do like this

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! FlipTableViewCell
    // TODO:
}
Sam
4#
Sam Reply to 2018-02-14 06:07:24Z

Just like all the other answers on this post, use the cellForRow(at: IndexPath) method on UITableView, to get a reference to the cells in the table view.

This method returns a UITableViewCell, if the cell is loaded (visible) and nil if the indexPath is not correct or the cell is not loaded.

The code should look like:

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

    let cell = tableView.cellForRow(at: indexPath)
    // Do something with cell.
}
Som Parkash
5#
Som Parkash Reply to 2018-02-14 07:12:07Z

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

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

© 2016 Powered by mzan.com design MATCHINFO