我有一个在自定义单元格中具有自定义按钮的应用程序。如果选择该单元格,它将显示到详细信息视图,这是完美的。如果我选择一个单元格中的按钮,则下面的代码将单元格索引打印到控制台中。
我需要访问所选单元格的内容(使用按钮)并将其添加到数组或字典中。我对此很陌生,因此很难找到如何访问单元格的内容。我尝试使用didselectrowatindexpath,但是我不知道如何将索引强制为标签的索引。
因此,基本上,如果每个单元格中有3个单元格带有“ Dog”,“ Cat”,“ Bird”作为cell.repeatLabel.text,并且我选择了第1行和第3行(索引0和2)中的按钮,在数组/字典中添加“狗”和“鸟”。
// 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)")
}
选项1.与委托一起处理
处理从单元的子视图触发的事件的正确方法是使用委托。
因此,您可以按照以下步骤操作:
1.在类定义上方,使用自定义单元格中的单个实例方法编写一个协议:
protocol CustomCellDelegate {
func cellButtonTapped(cell: CustomCell)
}
2.在类定义中声明一个委托变量,并在委托上调用protocol方法:
var delegate: CustomCellDelegate?
@IBAction func buttonTapped(sender: AnyObject) {
delegate?.cellButtonTapped(self)
}
3.遵循表视图所在的类中的CustomCellDelegate:
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.在视图控制器类中实现所需的方法。
编辑:首先定义一个空数组,然后像这样修改它:
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是在我的视图控制器中定义的数组:
private let items = ["Dog", "Cat", "Elephant", "Fox", "Ant", "Dolphin", "Donkey", "Horse", "Frog", "Cow", "Goose", "Turtle", "Sheep"]
选项2.使用闭包处理
我决定回来,向您展示另一种处理这类情况的方法。在这种情况下使用闭包将减少代码量,您将实现目标。
1.在单元格类中声明一个闭包变量:
var tapped: ((CustomCell) -> Void)?
2.调用按钮处理程序中的闭包。
@IBAction func buttonTapped(sender: AnyObject) {
tapped?(self)
}
3.在tableView(_:cellForRowAtIndexPath:)
包含视图控制器的类中:
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)")
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句