如何在 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
}
}
这是我的 .swift 代码后面的 xib 文件.
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 类.因此,由于未设置对象,它变得空了.
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屋!