Uitableview Swift-检索存储在自定义tableview单元格中的信息

Uitableview Swift-检索存储在自定义tableview单元格中的信息,uitableview,swift,uigesturerecognizer,custom-cell,didselectrowatindexpath,Uitableview,Swift,Uigesturerecognizer,Custom Cell,Didselectrowatindexpath,我已经在自定义单元格中实现了LongPress手势,现在我需要检索存储在这些单元格中的信息 我的cellForRowAtIndexPath函数如下所示: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let longPress: UILongPressGestureRecognizer = UILongPressGes

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

我的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
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

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

    messagType = selectedCell.messType

    println(messagType)

}
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
    }
}
返回麦克尔 }

我的didSelectRowAtIndexPath函数如下所示:

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
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

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

    messagType = selectedCell.messType

    println(messagType)

}
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
    }
}
我的cellLongPressed函数如下所示:

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
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

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

    messagType = selectedCell.messType

    println(messagType)

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

    }
}

如果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){

    }
}

您不会显示设置了
messType
的位置,也不会使用长按识别器执行任何操作。你能不能像cellForRow方法的其他部分那样,为这个问题添加更多的信息?作为一般建议,不要在单元格中“存储”数据。单元格显示模型中的数据,它们不应该用来存储东西。@jrturton我的函数现在已经添加,我希望这能让它更清晰。您不显示在哪里设置了
messType
,也不需要使用长按识别器。你能不能像cellForRow方法的其他部分那样,为这个问题添加更多的信息?作为一般建议,不要在单元格中“存储”数据。单元格显示模型中的数据,它们不应用于存储内容。@jrturton我的函数现在已经添加,我希望这能让它更清晰。也尝试过这个,但仍然无法打印任何内容。是否尝试长按?还是自来水@Frank21即使未在cellLongPressed函数中实现messagType,也不应该打印它吗?否。
didSelectRowAtIndexPath
如果长按,将不会被调用。只有当我们点击(比长按时间短)直到现在还没有看到您在答案中添加的内容时才会调用它,但它仍然有效:)非常感谢,特别是关于创建您自己的单元格指针的部分,我们将不胜感激!我也试过了,但还是没印出来。你试过长时间印刷吗?还是自来水@Frank21即使未在cellLongPressed函数中实现messagType,也不应该打印它吗?否。
didSelectRowAtIndexPath
如果长按,将不会被调用。只有当我们点击(比长按时间短)直到现在还没有看到您在答案中添加的内容时才会调用它,但它仍然有效:)非常感谢,特别是关于创建您自己的单元格指针的部分,我们将不胜感激!