5

我在cellForRowAtIndexPath里面有这个

   cell.plusBut.tag = indexPath.row
   cell.plusBut.addTarget(self, action: "plusHit:", forControlEvents: UIControlEvents.TouchUpInside)

这个函数在外面:

func plusHit(sender: UIButton!){
    buildings[sender.tag].something = somethingElse
}

是否可以发送indexPath.row and indexPath.section或其他替代方法?

谢谢!

编辑

我是这样接近它的:

我的自定义按钮

class MyButton: UIButton{

    var myRow: Int = 0
    var mySection: Int = 0

}

我的自定义单元格

class NewsCell: UITableViewCell{

    @IBOutlet weak var greenLike: MyButton!

在 CellForRowAtIndexPath 中

    cell.greenLike.myRow = indexPath.row

我在这条线上得到一个错误。

4

7 回答 7

18

是否可以发送 indexPath.row 和 indexPath.section 或其他替代方法?

如果您对节中可能的行数施加限制,则可以将两者合并为一个数字。例如,如果您愿意说给定部分中的行数总是少于 1000 行,则可以使用:

cell.plusBut.tag = (indexPath.section * 1000) + indexPath.row

然后使用 mod 和除法运算符来恢复:

row = sender.tag % 1000
section = sender.tag / 1000

另一种可能性是检查按钮的超级视图(以及它的超级视图等),直到找到单元格。获得单元格后,您可以从表中获取该单元格的索引路径。

第三种选择,也许是最好的选择,是让按钮以单元格而不是其他对象为目标。然后,单元格可以触发视图控制器或其他对象中的操作,并将其自身用作发送者。

于 2015-09-02T12:12:58.013 回答
4

您可以创建一个子类UIButton并在其中创建一个额外的属性部分。然后您可以将该类用于单元格按钮。您可以对行执行相同的操作。

以下是子类化后的几种可能方法UIButton

cell.plusBut.tag = indexPath.row
cell.plusBut.section = indexPath.section

或者

cell.plusBut.row = indexPath.row
cell.plusBut.section = indexPath.section

或者

cell.plusBut.indexPath = indexPath

选择适合你的。

于 2015-09-02T10:47:11.307 回答
3

这是我使用的一个非常简单的解决方案:

在 CellForRow 中:

button.tag = indexPath.row
cell.contentView.superview?.tag = indexPath.section

在函数中检索它是这样的:

func buttonTapped(_ sender: Any) {
    let button = sender as! UIButton
    let row = button.tag
    let sec = button.superview?.tag

    //retrieve item like self.array[sec][row]
    //do the rest of your stuff here
}
于 2018-09-25T23:09:50.917 回答
2

Swift 2.x - 带有关联对象的扩展

private struct AssociatedKeys {
static var section = "section"
static var row = "row"
}

extension UIButton {
var section : Int {
    get {
        guard let number = objc_getAssociatedObject(self,   &AssociatedKeys.section) as? Int else {
            return -1
        }
        return number
    }

    set(value) {
        objc_setAssociatedObject(self,&AssociatedKeys.section,Int(value),objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
}
var row : Int {
    get {
        guard let number = objc_getAssociatedObject(self, &AssociatedKeys.row) as? Int else {
            return -1
        }
        return number
    }

    set(value) {
        objc_setAssociatedObject(self,&AssociatedKeys.row,Int(value),objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
}
}

用法:

//set
cell.contextMenuBtn.section = indexPath.section
cell.contextMenuBtn.row = indexPath.row 
//read
func showContextMenu (sender:UIButton) {
    let track = dictionarySongs[sender.section][sender.row]
    ...
}
于 2016-10-14T11:15:34.130 回答
1

通过在Button中发送Row和Section可以使用tag和accessibilityIdentifier,看看:

设定值

 button.tag = indexPath.row
 button.accessibilityIdentifier = "\(indexPath.section)"

获得价值

let indexRow = button.tag
let indexSection = Int(button.accessibilityIdentifier!)
于 2018-04-02T16:51:28.610 回答
0

您可以通过扩展为 UIButton 分配自定义标签,看看:

extension UIButton {

private struct ButtonTag {

    static var tagKey = "key"

}

var myTag : (Int , Int)? {

    set  {
        if let value = newValue {
            objc_setAssociatedObject(self, &ButtonTag.tagKey, value as! AnyObject, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    get {
        return objc_getAssociatedObject(self, &ButtonTag.tagKey) as? (Int , Int)
    }
  }   
}
于 2016-09-05T13:23:55.517 回答
0

我建议另一种方法。我不喜欢子类解决方案,我认为按钮不应该知道它的位置。为什么不使用字典将按钮翻译成 IndexPath

// First initialize the lookup table
var buttonPositions = [UIButton: NSIndexPath]()

// add each button to the lookup table
let b = UIButton()
let path = NSIndexPath(forItem: 1, inSection: 1)


buttonPositions[b] = path

// Looking it up works like this
buttonPostions[activeButton]?.row  
于 2015-09-02T12:31:06.300 回答