Swift - 检索存储在自定义 tableview-cells 中的信息

Swift - Retrieving information stored in custom tableview-cells

我已经在我的自定义单元格中实现了 longPressGesture,现在我需要检索存储在这些单元格中的信息。

我的 cellForRowAtIndexPath 函数如下所示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "cellLongPressed:")
    longPress.delegate = self
    longPress.minimumPressDuration = 1
    longPress.numberOfTouchesRequired = 1

    let cellID = "cell"
    var mcell:CusCell = self.tv.dequeueReusableCellWithIdentifier("cell") as CusCell

    mcell.addGestureRecognizer(longPress)

    let data = mainList[indexPath.row] as SecondModel

    var dateStr:String = String()
        dateStr = printDate(data.date)

        mcell.mainLabel.text = data.receiver
        mcell.recLabel.text = "Message sent at \(dateStr)"
        mcell.imageLabel.image = UIImage(named: icons[0])
        mcell.messType = messageType

返回 mcell }

我的 didSelectRowAtIndexPath 函数如下所示:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let selectedCell: CusCell = tv.cellForRowAtIndexPath(indexPath) as CusCell

    messagType = selectedCell.messType

    println(messagType)

}

我的 cellLongPressed 函数如下所示:

func cellLongPressed(gestureRecognizer:UIGestureRecognizer) {

    if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {
        println("STATE ENDED")
        //Do Whatever You want on End of Gesture
    }
    else if (gestureRecognizer.state == UIGestureRecognizerState.Began){
        println("STATE BEGAN")
        //Do Whatever You want on Began of Gesture
    }
}

现在您可能已经猜到了,存储在 "selectedCell.messType" 中的整数永远不会被打印出来。我真的不明白为什么这不起作用,我对 "selectedCell" 的声明是错误的吗?

如有任何建议,我们将不胜感激。

如果 messageType 是模型的一部分,为什么不能从模型中读回数据?从单元读回数据不是一个好方法。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)  {

    let data = mainList[indexPath.row] as SecondModel
    var messagType = data.messageType
    println(messagType)

}

如果您想记录长按事件中的值

func cellLongPressed(gestureRecognizer:UIGestureRecognizer) {

    if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {
        var point = gestureRecognizer.locationInView(self.tableView)
        if let indexPath = self.tableView.indexPathForRowAtPoint(point)
        {
            let data = mainList[indexPath.row] as SecondModel
            var messagType = data.messageType
            println(messagType)
        }
    }
    else if (gestureRecognizer.state == UIGestureRecognizerState.Began){

    }
}