我有一个应用程序,在自定义单元格中有自定义按钮。如果您选择单元格,它将转移到详细视图,这是完美的。如果我在单元格中选择一个按钮,下面的代码会将单元格索引打印到控制台中。
我需要访问所选单元格的内容(使用按钮)并将它们添加到数组或字典中。我是新手,因此很难找到如何访问单元格的内容。我尝试使用didselectrowatindexpath,但我不知道如何强制索引是标记的索引...
所以基本上,如果有3个单元格中有'Dog','Cat','Bird'作为每个单元格中的cell.repeatLabel.text,我选择第1行和第3行(索引0和2)中的按钮,它应该将'Dog'和'Bird'添加到数组/字典中。
// 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.在类定义中声明一个委托变量并在委托上调用协议方法:
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)")
}
由于表视图中有1个部分,因此可以获取单元格对象,如下所示。
let indexPath = NSIndexPath(forRow: tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell!
你可以从按钮标签获得标签。
Swift 3我只是使用按钮标签的superview在@IBAction函数中获取访问单元的解决方案。
let cell = sender.superview?.superview as! ProductCell
var intQty = Int(cell.txtQty.text!);
intQty = intQty! + 1
let strQty = String(describing: intQty!);
cell.txtQty.text = strQty
@IBAction func buttonTap(sender: UIButton) {
let button = sender as UIButton
let indexPath = self.tableView.indexPathForRowAtPoint(sender.center)!
}
我更新了Vasil Garov为Swift 3提供答案的选项1
1.为您的CustomCell
创建协议:
protocol CustomCellDelegate {
func cellButtonTapped(cell: CustomCell)
}
2.为你的TableViewCell
声明一个delegate
变量并在其上调用协议方法:
var delegate: CustomCellDelegate?
@IBAction func buttonTapped(sender: AnyObject) {
delegate?.cellButtonTapped(self)
}
3.符合你的CustomCellDelegate
所在班级的tableView
:
class ViewController: CustomCellDelegate
4.设置你的细胞的delegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as CustomCell
cell.delegate = self
return cell
}
5.在ViewController
中实现所需的方法。
根据Cao的回答,这是一个处理集合视图单元格中的按钮的解决方案。
@IBAction func actionButton(sender: UIButton) {
let point = collectionView.convertPoint(sender.center, fromView: sender.superview)
let indexPath = collectionView.indexPathForItemAtPoint(point)
}
请注意,convertPoint()函数调用将转换集合视图空间中的按钮点坐标。如果没有,indexPath将始终引用相同的单元号0
XCODE 8:重要说明
不要忘记将标记设置为不同于0的值。
如果你试图使用tag = 0来转换对象,它可能会起作用,但在某些奇怪的情况下它不会。
修复方法是将标记设置为不同的值。希望它可以帮助某人。