[英]Get text and button in cell in UITableView - Swift 3
我正在尝试在第二个视图控制器上使用的每个单元格中实现一个按钮(请参见图像)。 该按钮应该执行一些代码,但我似乎还无法弄清楚如何将按钮放入其中。 我真的不能制作带有按钮的新商品吗? 我曾尝试在SO上进行搜索,但是大多数代码都是“旧的”,关于该代码我有很多不了解的地方仍需要更改。
在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()
}
在SecondViewContoller上是:
class SecondViewController: UIViewController {
@IBOutlet weak var input: UITextField!
@IBAction func addItem(_ sender: Any) {
if ( input.text != "") {
list.append(input.text!)
input.text = ""
}
}
该代码来自TheSwiftGuy,我什么都不拥有,只想尝试向其中添加自己的功能! ^^
为什么每个单元格上都需要一个按钮,您可以实现默认的表视图方法。
didSelectRowAtIndexPath
这样你可以解决你的问题
据我了解,您的问题是您需要将单元格的按钮传递到另一个屏幕。 对 ?
好吧,我已经完成了可能有帮助的代码。
//
// 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.
}
}
另外,某些屏幕可以帮助您理解和重复代码。
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.