Uitableview 如何使用按钮标签访问swift中自定义单元格的内容?

Uitableview 如何使用按钮标签访问swift中自定义单元格的内容?,uitableview,swift,uibutton,tags,tableviewcell,Uitableview,Swift,Uibutton,Tags,Tableviewcell,我有一个在自定义单元格中有自定义按钮的应用程序。如果选择单元,它将与详图视图相连接,这非常完美。如果我在单元格中选择一个按钮,下面的代码将单元格索引打印到控制台中 我需要访问所选单元格的内容(使用按钮)并将其添加到数组或字典中。我是新手,所以很难找到如何访问手机的内容。我尝试使用didselectrowatindexpath,但我不知道如何强制索引为标记的索引 因此,基本上,如果有3个单元格中的cell.repeatLabel.text为'Dog'、'Cat'、'Bird',并且我选择了第1行和

我有一个在自定义单元格中有自定义按钮的应用程序。如果选择单元,它将与详图视图相连接,这非常完美。如果我在单元格中选择一个按钮,下面的代码将单元格索引打印到控制台中

我需要访问所选单元格的内容(使用按钮)并将其添加到数组或字典中。我是新手,所以很难找到如何访问手机的内容。我尝试使用didselectrowatindexpath,但我不知道如何强制索引为标记的索引

因此,基本上,如果有3个单元格中的cell.repeatLabel.text为'Dog'、'Cat'、'Bird',并且我选择了第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个部分,因此可以获得如下所示的单元对象

let indexPath = NSIndexPath(forRow: tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell!

您将从按钮标签中获得标签的位置。

选项1。通过授权处理它

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
处理从单元格子视图触发的事件的正确方法是使用委派

因此,您可以按照以下步骤操作:

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是在my view controller中定义的数组:

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.在包含视图控制器类的
表格视图(uuxCELLFORROWATINDEXPATH:)
中:

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)")
}

根据Cao的回答,下面是一个处理集合视图单元格中按钮的解决方案

    @IBAction func actionButton(sender: UIButton) {
        let point = collectionView.convertPoint(sender.center, fromView: sender.superview)
        let indexPath = collectionView.indexPathForItemAtPoint(point)
}
请注意,convertPoint()函数调用将转换集合视图空间中的按钮点坐标。如果没有,indexPath将始终引用相同的单元格编号0

Swift 3 我刚刚使用按钮标签的superview获得了@iAction函数中访问单元的解决方案

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

我更新了Vasil Garov回答的选项1,用于Swift 3

1.为您的
CustomCell
创建协议:

protocol CustomCellDelegate {
    func cellButtonTapped(cell: CustomCell)
} 
2.对于您的
TableViewCell
声明一个
委托
变量并调用其上的协议方法:

var delegate: CustomCellDelegate?

@IBAction func buttonTapped(sender: AnyObject) {
    delegate?.cellButtonTapped(self)
}
3.在您的
tableView
为以下内容的类中遵守
CustomCellDelegate

 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.
视图控制器中实现所需的方法

代码8:重要提示

不要忘记将标记设置为与0不同的值

如果您试图强制转换tag=0的对象,它可能会工作,但在某些奇怪的情况下,它不会

解决方法是将标记设置为不同的值。
希望它能帮助别人。

我更喜欢这个解决方案。但是,您忘了提到他必须实际设置代理。除此之外,解决方案是可靠的。当然,我应该有,我的错。我将编辑它。谢谢,非常感谢你们。它很有魅力,让我对代表有了一点了解。。我还是个新手。我知道我现在正在努力,但我一直在兜圈子。。我需要能够将selectedItem添加到数组中,但我不知道如何添加。我试着把它加入cellButtonTapped基金,但那不起作用。如果我选择了某个对象,它必须将其添加到数组中,如果我取消选择它,它必须将其从数组中删除。这个想法是,一旦我有了数组,当用户单击“发送”按钮时,我将把所选项目的结果发布到mySQL数据库中……您在
cellButtonTapped
中尝试了什么?而预期的行为到底是什么-点击按钮->向数组中添加项,再次点击->从数组中删除项?即使只有一个部分,它也不会每次都起作用。假设您从tableview中删除了一个单元格,并且如果您以indexPath访问标记,它将为您提供错误的索引。因此请注意。。。