获取UITableView-Swift 3中单元格中的文本和按钮

获取UITableView-Swift 3中单元格中的文本和按钮,swift,button,cell,Swift,Button,Cell,我试图在每个单元格中实现一个按钮,该按钮由在第二个视图控制器上使用的创建(请参见图片)。这个按钮应该可以做一些代码,但我似乎不知道如何把按钮也放在那里。我真的不能用按钮创建一个新的项目数组,对吗? 我已经试着搜索过了,但是大部分代码都是“旧的”,关于这些代码还有很多我不理解的地方需要修改 在FirstViewController上,它是: import UIKit var list = ["Buy milk", "mow the lawn", "Run"] class FirstViewC

我试图在每个单元格中实现一个按钮,该按钮由在第二个视图控制器上使用的创建(请参见图片)。这个按钮应该可以做一些代码,但我似乎不知道如何把按钮也放在那里。我真的不能用按钮创建一个新的项目数组,对吗? 我已经试着搜索过了,但是大部分代码都是“旧的”,关于这些代码还有很多我不理解的地方需要修改

在FirstViewController上,它是:

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()
}
class SecondViewController: UIViewController {

@IBOutlet weak var input: UITextField!

@IBAction func addItem(_ sender: Any) {
    if ( input.text != "") {
        list.append(input.text!)
        input.text = ""
    }
}
在SecondViewController上,它是:

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()
}
class SecondViewController: UIViewController {

@IBOutlet weak var input: UITextField!

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

这段代码来自于软件商,我没有任何代码,只是想尝试添加我自己的功能^^

据我所知,您的问题是需要单元格的按钮才能转到另一个屏幕。对吧?

嗯,我已经完成了可能会有所帮助的代码

//
//  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.
    }
}
还有一些屏幕可以帮助您理解和重复代码


谢谢。

据我所知,您的问题是需要单元格的按钮才能转到另一个屏幕。对吧?

嗯,我已经完成了可能会有所帮助的代码

//
//  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.
    }
}
还有一些屏幕可以帮助您理解和重复代码


谢谢。

为什么每个单元格上都需要一个按钮?您可以实现默认设置 表视图方法,即

didSelectRowAtIndexPath

有了它,您可以解决您的问题

为什么您需要在每个单元格上设置一个按钮,或者您可以实现默认设置 表视图方法,即

didSelectRowAtIndexPath


有了这个你就可以解决你的问题了

我想这就是问题所在!我一到家就测试一下,会让你知道的!不,这不是我的意思。我只是看到我的标题错了,这可能会让问题变得不同。在具有TableView的ViewController中,我希望文本位于左侧,按钮位于右侧。我可以让文本显示(我的代码),但是按钮应该在同一个单元格中,而不是在代码中的不同ViewController中。所以再次强调:我想要一个包含文本和按钮的单元格,并且该按钮链接到ViewContoller.swift,以便它可以更改按钮所在单元格的某些属性。我想这可能就是它!我一到家就测试一下,会让你知道的!不,这不是我的意思。我只是看到我的标题错了,这可能会让问题变得不同。在具有TableView的ViewController中,我希望文本位于左侧,按钮位于右侧。我可以让文本显示(我的代码),但是按钮应该在同一个单元格中,而不是在代码中的不同ViewController中。同样,我想要一个包含文本和按钮的单元格,该按钮链接到ViewContoller.swift,以便它可以更改按钮所在单元格的某些属性。Eric Aya是对的,您不需要在每个单元格中都使用按钮。如果你因为某种原因。。。您需要使用自定义的tableview单元格。看这里,谢谢Devster101,我会查一下,因为我确实需要每个单元格中都有一个按钮。这个按钮不是用来添加单元格的:)我的意思是说@swapnil是对的,不是Eric。这看起来像是你在制作某种notes应用程序,你在SecondViewController中向列表中添加了一个新项目,但单元格中的按钮具体会做什么?该按钮将重置触发通知的计时器。每个单元格都需要自己的计时器+通知,因此需要自己的按钮。我明白了。下一步可能会遇到困难,因此一旦创建了自定义单元格,就需要为firstViewController也符合的单元格创建协议。Eric Aya是对的,你不需要每个单元格都有一个按钮。如果你因为某种原因。。。您需要使用自定义的tableview单元格。看这里,谢谢Devster101,我会查一下,因为我确实需要每个单元格中都有一个按钮。这个按钮不是用来添加单元格的:)我的意思是说@swapnil是对的,不是Eric。这看起来像是你在制作某种notes应用程序,你在SecondViewController中向列表中添加了一个新项目,但单元格中的按钮具体会做什么?该按钮将重置触发通知的计时器。每个单元格都需要自己的计时器+通知,因此需要自己的按钮。我明白了。下一步可能会遇到困难,因此一旦创建了自定义单元格,就需要为firstViewController也符合的单元格创建协议。看到类似的情况了吗?但是我如何在单元格中放置一个带有该代码的按钮呢?不,您不需要在其中放置按钮。只需实现didSelectAtIndexPath方法,并显示第二个控制器,它将重定向到该pagefunc tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){var secondVC=self.storyboard?.instanceeviewcontroller(带有标识符:“SecondView storyboard Identifier”)作为secondVC self.present(secondVC,动画:true,完成:nil)}好的?但是我如何在单元格中放置一个带有该代码的按钮呢?不,您不需要在其中放置一个按钮。只需实现didSelectAtIndexPath方法并显示第二个控制器,它将重定向到该pagefunc tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){var secondVC=self.storyboard?.instanceeviewcontroller(带有标识符:“SecondView storyboard Identifier”)作为secondVC self.present(secondVC,动画:true,完成:nil)}