martin
martin

Reputation: 1974

Swift - Retrieving information stored in custom tableview-cells

I've implemented the longPressGesture in my custom cells, and now I need to retrieve the information that is stored in these cells.

My cellForRowAtIndexPath-function looks like this:

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

return mcell }

My didSelectRowAtIndexPath-function looks like this:

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

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

    messagType = selectedCell.messType

    println(messagType)

}

My cellLongPressed-function looks like this:

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

Now as you probably can guess, the integer stored in "selectedCell.messType" never gets printed. I don't really understand why this shouldn't work, is my declaration of the "selectedCell" wrong?

Any suggestions would be appreciated.

Upvotes: 0

Views: 1489

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

Why can't you read the data back from the model if messageType is part of the model? Reading the data back from the cell is not a good way to do it.

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

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

}

If its you want to log the values inside long press event

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

    }
}

Upvotes: 4

Related Questions