如何在Swift中的UITableViewCell上添加带有单击事件的按钮? [英] How to add a button with click event on UITableViewCell in Swift?
问题描述
在我的主页中,我为UITableViewCell创建了一个xib文件.我正在从该xib文件加载单元格,并且工作正常.
In my main page, I created a xib file for UITableViewCell. I'm loading the cell from that xib file and its working fine.
在单元格中,我有一些标签和按钮.我的目的是通过单击单元格上的按钮来更改标签.
Inside of the cell I have some labels and buttons. I'm aiming to change the label by clicking to the button on the cell.
我的代码在下面喜欢
import UIKit
class SepetCell: UITableViewCell{
@IBOutlet var barcode: UILabel!
@IBOutlet var name: UILabel!
@IBOutlet var fav: UIButton!
@IBOutlet var strep: UIStepper!
@IBOutlet var times: UILabel!
@IBAction func favoriteClicked(sender: UIButton) {
println(sender.tag)
println(times.text)
SepetViewController().favorite(sender.tag)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这是我的xib文件,其后缀为.swift.
This is my xib files behind codes as .swift.
主页中的代码如下:
import UIKit
import CoreData
class SepetViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
@
IBOutlet
var sepetTable: UITableView!
var barcodes: [CART] = []
let managedObjectContext = (UIApplication.sharedApplication().delegate as!AppDelegate).managedObjectContext
override func viewWillAppear(animated: Bool) {
if let moc = self.managedObjectContext {
var nib = UINib(nibName: "SepetTableCell", bundle: nil)
self.sepetTable.registerNib(nib, forCellReuseIdentifier: "productCell")
}
fetchLog()
sepetTable.reloadData()
}
func fetchLog() {
if let moc = self.managedObjectContext {
barcodes = CART.getElements(moc);
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int {
return self.barcodes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("productCell") as ? SepetCell
if cell == nil {
println("cell nil")
}
let product: CART
product = barcodes[indexPath.row]
cell!.barcode ? .text = product.barcode
cell!.name ? .text = product.name
cell!.fav.tag = indexPath.row
return cell!
}
func favorite(tag: Int) {
}
}
当我单击单元"内部的收藏夹按钮时.例如,我想将时间标签文本更改为任何内容.
When i clicked fav button inside of the Cell. I wanted to change times label text to anything for example.
当我单击fav按钮时,该事件将转到SepetCell.swift favoriteClicked(sender:UIButton)函数.
When I clicked to the fav button, the event will gone to the SepetCell.swift favoriteClicked(sender: UIButton) function.
因此,如果我尝试致电: SepetViewController().favorite(sender.tag)
So if i try to call: SepetViewController().favorite(sender.tag)
它将进入
func favorite(tag: Int) {
sepetTable.reloadData()
}
但是sepetTable到那里时为零.我认为这是因为当我调用此SepetViewController().favorite(sender.tag)函数时.首先创建SepetViewController类.因此,由于未设置对象,因此它为null.
but sepetTable is nil when it is gone there. I think it is because of when I call this SepetViewController().favorite(sender.tag) function. It firstly creates SepetViewController class. So because of object is not setted it is getting null.
我如何到达该sepetTable或解决此问题的最佳方法是什么?
How can I reach that sepetTable or what is the best way to solve this issue.
谢谢.
推荐答案
用于解决此问题的流行模式是闭包和委托. 如果要使用闭包,则可以执行以下操作:
Popular patterns for solving this problem are closures and delegates. If you want to use closures, you would do something like this:
final class MyCell: UITableViewCell {
var actionBlock: (() -> Void)? = nil
然后
@IBAction func didTapButton(sender: UIButton) {
actionBlock?()
}
然后在您的tableview委托中:
then in your tableview delegate:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as? MyCell
cell?.actionBlock = {
//Do whatever you want to do when the button is tapped here
}
一种流行的替代方法是使用委托模式:
A popular alternative is to use the delegate pattern:
protocol MyCellDelegate: class {
func didTapButtonInCell(_ cell: MyCell)
}
final class MyCell: UITableViewCell {
weak var delegate: MyCellDelegate?
然后
@IBAction func didTapButton(sender: UIButton) {
delegate?.didTapButtonInCell(self)
}
.. 现在在您的视图控制器中:
.. Now in your view controller:
然后在您的tableview委托中:
then in your tableview delegate:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as? MyCell
cell?.delegate = self
并向协议添加一致性,如下所示:
And add conformance to the protocol like this:
extension MyViewController: MyCellDelegate {
didTapButtonInCell(_ cell: MyCell) {
//Do whatever you want to do when the button is tapped here
}
}
希望这会有所帮助!
这篇关于如何在Swift中的UITableViewCell上添加带有单击事件的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!