如何使用按钮标签快速访问自定义单元格的内容? [英] How to access the content of a custom cell in swift using button tag?
问题描述
我有一个应用,在自定义单元格中有一个自定义按钮.如果您选择单元格,它会转到详细视图,这是完美的.如果我在单元格中选择一个按钮,下面的代码会将单元格索引打印到控制台中.
I have an app that has a custom button in a custom cell. If you select the cell it segues to the a detail view, which is perfect. If I select a button in a cell, the code below prints the cell index into the console.
我需要访问所选单元格的内容(使用按钮)并将它们添加到数组或字典中.我是新手,所以很难找到如何访问单元格的内容.我尝试使用 didselectrowatindexpath,但我不知道如何强制索引成为标签的索引...
I need to access the contents of the selected cell (Using the button) and add them to an array or dictionary. I am new to this so struggling to find out how to access the contents of the cell. I tried using didselectrowatindexpath, but I don't know how to force the index to be that of the tag...
所以基本上,如果有 3 个单元格,每个单元格中的 cell.repeatLabel.text 为 'Dog'、'Cat'、'Bird',然后我选择第 1 行和第 3 行(索引 0 和 2)中的按钮,它应该将 'Dog' 和 'Bird' 添加到数组/字典中.
So basically, if there are 3 cells with 'Dog', 'Cat', 'Bird' as the cell.repeatLabel.text in each cell and I select the buttons in the rows 1 and 3 (Index 0 and 2), it should add 'Dog' and 'Bird' to the array/dictionary.
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postsCollection.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
// Configure the cell...
var currentRepeat = postsCollection[indexPath.row]
cell.repeatLabel?.text = currentRepeat.product
cell.repeatCount?.text = "Repeat: " + String(currentRepeat.currentrepeat) + " of " + String(currentRepeat.totalrepeat)
cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
cell.checkButton.tag = indexPath.row;
cell.checkButton.addTarget(self, action: Selector("selectItem:"), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func selectItem(sender:UIButton){
println("Selected item in row \(sender.tag)")
}
推荐答案
OPTION 1. 使用委托
处理从单元格子视图触发的事件的正确方法是使用委托.
The right way of handling events fired from your cell's subviews is to use delegation.
因此您可以按照以下步骤操作:
So you can follow the steps:
1. 在您的类定义上方编写一个协议,在您的自定义单元格中使用单个实例方法:
1. Above your class definition write a protocol with a single instance method inside your custom cell:
protocol CustomCellDelegate {
func cellButtonTapped(cell: CustomCell)
}
2. 在您的类定义中声明一个委托变量并在委托上调用协议方法:
2. Inside your class definition declare a delegate variable and call the protocol method on the delegate:
var delegate: CustomCellDelegate?
@IBAction func buttonTapped(sender: AnyObject) {
delegate?.cellButtonTapped(self)
}
3. 符合表视图所在类中的 CustomCellDelegate:
3. Conform to the CustomCellDelegate in the class where your table view is:
class ViewController: CustomCellDelegate
4.设置单元格的代表
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomCell
cell.delegate = self
return cell
}
5. 在您的视图控制器类中实现所需的方法.
5. Implement the required method in your view controller class.
首先定义一个空数组,然后像这样修改它:
First define an empty array and then modify it like this:
private var selectedItems = [String]()
func cellButtonTapped(cell: CustomCell) {
let indexPath = self.tableView.indexPathForRowAtPoint(cell.center)!
let selectedItem = items[indexPath.row]
if let selectedItemIndex = find(selectedItems, selectedItem) {
selectedItems.removeAtIndex(selectedItemIndex)
} else {
selectedItems.append(selectedItem)
}
}
其中 items 是在我的视图控制器中定义的数组:
where items is an array defined in my view controller:
private let items = ["Dog", "Cat", "Elephant", "Fox", "Ant", "Dolphin", "Donkey", "Horse", "Frog", "Cow", "Goose", "Turtle", "Sheep"]
选项 2. 使用闭包
我决定回来向您展示处理此类情况的另一种方法.在这种情况下使用闭包将减少代码,您将实现目标.
I've decided to come back and show you another way of handling these type of situations. Using a closure in this case will result in less code and you'll achieve your goal.
1. 在您的单元格类中声明一个闭包变量:
1. Declare a closure variable inside your cell class:
var tapped: ((CustomCell) -> Void)?
2. 在按钮处理程序中调用闭包.
2. Invoke the closure inside your button handler.
@IBAction func buttonTapped(sender: AnyObject) {
tapped?(self)
}
3. 在包含视图控制器类的 tableView(_:cellForRowAtIndexPath:)
中:
3. In tableView(_:cellForRowAtIndexPath:)
in the containing view controller class :
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
cell.tapped = { [unowned self] (selectedCell) -> Void in
let path = tableView.indexPathForRowAtPoint(selectedCell.center)!
let selectedItem = self.items[path.row]
println("the selected item is \(selectedItem)")
}
这篇关于如何使用按钮标签快速访问自定义单元格的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!