简体   繁体   中英

Get text and button in cell in UITableView - Swift 3

I'm trying to implement a button in each cell that is created by the used on a second view controller (see images). This button should do some code but I just can't seem to figure out how to get the button in there as well. Can't really make a new array of items with buttons, can I? I've tried searching on SO but most of the code is 'old' and there are a lot of things that I don't understand about that code that still need to be changed..

在此处输入图片说明

On FirstViewController it is:

import UIKit

var list = ["Buy milk", "mow the lawn", "Run"]

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    cell.textLabel?.text = list[indexPath.row]

    return(cell)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        list.remove(at: indexPath.row)
        myTableView.reloadData()
    }
}

override func viewDidAppear(_ animated: Bool) {
    myTableView.reloadData()
}

On SecondViewContoller it is:

class SecondViewController: UIViewController {

@IBOutlet weak var input: UITextField!

@IBAction func addItem(_ sender: Any) {
    if ( input.text != "") {
        list.append(input.text!)
        input.text = ""
    }
}

The code is from TheSwiftGuy and I don't own any of it, just wanted to try and add my own features to it! ^^

why you need a button on each cell either you can implement default table view method that is.

didSelectRowAtIndexPath

with this you can solve your problem

As far I understood your problem is you need cell's button to pass to another screen. right ?

Well, I've done the code that might help.

//
//  BtnDemoTVC.swift
//  ChartsDemo
//
//  Created by Vivek on 10/4/2017.
//  Copyright © 2017 Vivek. All rights reserved.
//

import Foundation
import UIKit

class customBtn : UIButton {
    var property : String?
}

protocol BtnDemoTVC_Cell_protocol {
    func btnTapped(btn : customBtn)
}

class BtnDemoTVC_Cell : UITableViewCell {

    @IBOutlet weak var btn: customBtn!

    var delegate : BtnDemoTVC_Cell_protocol?

    @IBAction func btnTapped(_ sender: customBtn) {
        delegate?.btnTapped(btn: sender)
    }
}

class BtnDemoTVC : UITableViewController {
    //-----------------------------------------------------------------------
    //MARK: - Outlets

    //-----------------------------------------------------------------------
    //MARK: - Variables

    var selectedButton : customBtn?

    //-----------------------------------------------------------------------
    //MARK: - Memory Management Methods

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //----------------------------------------------------------------------
    //MARK: - Custom methods

    func setUpVC() {

        dataEntry_API()
    }

    //------------------------------------------------------

    func dataEntry_API() {

    }

    //----------------------------------------------------------------------
    //MARK: - Action Method

    //-----------------------------------------------------------------------
    //MARK: - View Life Cycle Methods

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //----------------------------------------------------------------------

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setUpVC()
    }

    //----------------------------------------------------------------------

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
}

extension BtnDemoTVC {
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BtnDemoTVC_Cell", for: indexPath) as! BtnDemoTVC_Cell
        cell.delegate = self
        cell.btn.property = "property No.\(indexPath.row + 1)"
        return cell
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
}

extension BtnDemoTVC : BtnDemoTVC_Cell_protocol{
    func btnTapped(btn: customBtn) {
        selectedButton = btn
        print("\(selectedButton?.property)")
        //Pass this button on navigated page.
    }
}

Also Some screens that can help you to understand and repeat the code.

在此处输入图片说明

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM