Swift — Need to click and drag a UIImageView inside a Custom TableView Cell

By Rick Segrest

I am trying to create a custom TableViewController with a custom UITableViewCell class. I need to be able to drag and drop a photo inside the cell to a panel at the bottom of the view. I have tried a few ways to do this, but I am running into a couple of problems and haven’t found a complete solution online:

First of all, after dragging and dropping I would like to return the UIImage view to the cell it came from.

Secondly, I am not able to cast a ‘view’ to my custom UITableViewClass outside the ‘cellForRowAtIndexPath’ method (so I will have access to the outlets I have set up.)

Here is my relevant code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let thisCell:MealPlanTableViewCell = tableView.dequeueReusableCellWithIdentifier("mealPlanCell", forIndexPath: indexPath) as! MealPlanTableViewCell
    let meal = mealPlanElementArray[indexPath.row]
    thisCell.postTextLabel.text = meal.2
    thisCell.foodImageView.image = meal.4
    let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
    thisCell.foodImageView.addGestureRecognizer(gesture)
    thisCell.foodImageView.userInteractionEnabled = true
    return thisCell
}
func wasDragged(gesture: UIPanGestureRecognizer) {
    if calendarIsUp == calendarIsUp {
        let meal = gesture.view! as! UIImageView
        let sview = gesture.view!.superview!.superview!
        // CAN'T DO THE FOLLOWING -- CAN'T CAST TO CUSTOM CELL
        let mptvc:MealPlanTableViewCell = gesture.view!.superview!.superview! as! MealPlanTableViewCell
        if beginningDrag == true
        {
            beginningDrag = false
            // Move
            meal.superview?.superview?.superview?.superview?.superview?.superview?.superview?.addSubview(meal)
            beforeDragX = (meal.center.x)
            beforeDragY = (meal.bounds.maxY)
        }
        let translation = gesture.translationInView(self.view)
        meal.center = CGPoint(x: beforeDragX + translation.x, y: beforeDragY + translation.y)
        if gesture.state == UIGestureRecognizerState.Ended {
            beginningDrag = true
            // NEED TO ADD BACK TO custom UITableViewCell class.
            sview.addSubview(meal)
            meal.center = CGPoint(x: beforeDragX, y: beforeDragY)
        }
    }

Thanks for the help…

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273