Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过按钮Swift中的标记发送行和节_Ios_Swift_Uitableview_Button_Sender Id - Fatal编程技术网

Ios 通过按钮Swift中的标记发送行和节

Ios 通过按钮Swift中的标记发送行和节,ios,swift,uitableview,button,sender-id,Ios,Swift,Uitableview,Button,Sender Id,我在cellForRowAtIndexPath中有这个 cell.plusBut.tag = indexPath.row cell.plusBut.addTarget(self, action: "plusHit:", forControlEvents: UIControlEvents.TouchUpInside) cell.greenLike.myRow = indexPath.row 而这个功能在外部: func plusHit(sender: UIButton!){

我在cellForRowAtIndexPath中有这个

   cell.plusBut.tag = indexPath.row
   cell.plusBut.addTarget(self, action: "plusHit:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.greenLike.myRow = indexPath.row
而这个功能在外部:

func plusHit(sender: UIButton!){
    buildings[sender.tag].something = somethingElse
}
是否可以发送
indexPath.row
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
}
我的自定义按钮

class MyButton: UIButton{

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

}
我的自定义单元格

class NewsCell: UITableViewCell{

    @IBOutlet weak var greenLike: MyButton!
在CellForRowAtIndexPath中

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

这一行出现错误。

您可以创建
UIButton
的子类,并在其中创建一个额外的属性部分。然后你就可以用这个类来做单元格按钮了。您可以对row执行相同的操作

在子类化
UIButton

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

选择适合你的

是否可以发送indexPath.row和indexPath.section,或其他替代文件

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

cell.plusBut.tag = (indexPath.section * 1000) + indexPath.row
然后使用mod和division运算符恢复:

row = sender.tag % 1000
section = sender.tag / 1000
另一种可能是检查按钮的superview(以及它的superview等),直到找到单元格。一旦有了单元格,就可以从表中获取该单元格的索引路径


第三种选择,也许是最好的选择,是让按钮指向单元格而不是其他对象。然后,该单元格可以在视图控制器或其他对象中触发一个操作,将其自身用作发送方。

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

// 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  

您可以通过扩展为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)
    }
  }   
}

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]
    ...
}

通过in按钮发送行和节您可以使用tag和accessibilityIdentifier,查看:

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)
    }
  }   
}
设定值

 button.tag = indexPath.row
 button.accessibilityIdentifier = "\(indexPath.section)"
获得价值

let indexRow = button.tag
let indexSection = Int(button.accessibilityIdentifier!)

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

在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
}

请看上面我的错误。。。不知道为什么会发生这是一个很好的解决方案。。。我也试着做一些类似的事情。显然,节和行都被假定为整数。请原谅这个问题,但我不明白。如果section是2,我乘以1000,加上行,例如5,我将得到2005。如果我除以1000,我将得到2.005,也就是2。对的但是行?@Markus如果
sender.tag
是2005,那么
row=sender.tag%1000
,而
2005%1000
是5
%
是模运算符,它在第一个操作数除以第二个操作数后给出余数
2005/1000=2,余数5
,所以
2005%1000
是5。@Markus还注意到,因为我们谈论的是整数,所以2005/1000=2,而不是2.005。这应该是可以接受的答案!我认为第二行代码应该替换为
cell.button.superview?.tag=indexPath.section
否则,它将显示0节。