我已经安装了类似Instagram的应用程序,希望能够在图片上左右滑动,这样我就可以表示喜欢和不喜欢。为什么我的代码在运行时不工作?它不加载并突出显示

 myCell.postedImage.addGestureRecognizer(swipeRight)

这是我的tableView代码
import UIKit
import Parse


class HomePage: UITableViewController {

var images = [UIImage]()
var titles = [String]()
var imageFile = [PFFile]()




   override func viewDidLoad() {
    super.viewDidLoad()

    println(PFUser.currentUser())

    var query = PFQuery(className:"Post")

    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {

            println("Successfully retrieved \(objects!.count) scores.")

            for object in objects! {

                self.titles.append(object["Title"] as! String)

                self.imageFile.append(object["imageFile"] as! PFFile)

                self.tableView.reloadData()

            }
        } else {
            // Log details of the failure
            println(error)
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return titles.count


}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 500

}

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

    var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! cell

    myCell.rank.text = "21"
    myCell.votes.text = "4012"
    myCell.postDescription.text = titles[indexPath.row]

    imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

        if let downloadedImage = UIImage(data: data!) {

            myCell.postedImage.image = downloadedImage

        }
    }

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    myCell.postedImage.addGestureRecognizer(swipeRight)



    return myCell

}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                println("Swiped right")
                default:
                break
            }
        }
    }

}

我该怎么做才能让刷卡动作生效?

最佳答案

将imageview的用户交互设置为true,以便手势操作在图像上执行

var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
myCell.postedImage.userInteractionEnabled = true;
myCell.postedImage.addGestureRecognizer(swipeRight)

如果您使用自定义单元格,可以从XIB中执行此操作。

10-08 07:31