使用滑动操作更改UIImageView图像

时间:2016-05-23 12:40:36

标签: ios swift uitableview uiimageview swift2

在我的tableview上我有一个UIImageView,我需要在执行滑动操作时更改图像。我有一个类到tableview的行,所以我改变了代码来尝试这样做。这是代码,但没有奏效。我该怎么办?

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) -> CellTableViewFriends{  
    // you need to implement this method too or you can't swipe to display the actions  
    let cell = self.tableViewFriends.dequeueReusableCellWithIdentifier("CellTableViewFriends") as! CellTableViewFriends;  
    cell.imgArrow.image = UIImage(named: "myImage")  

    return cell  
}  

3 个答案:

答案 0 :(得分:1)

在您的单元格类中,您可以添加以下方法:

func addSwipeGesture() {
        let gestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedLeft))
        gestureRecognizer.direction = .Left
        self.addGestureRecognizer(gestureRecognizer)
    }

    func swipedLeft(gesture: UIGestureRecognizer!) {
        //Change picture here
        imgArrow.image = UIImage(named: "myImage")
    }

您可以更改方向或执行相同操作以添加多个方向。

然后你应该确保在创建单元格时调用addSwipeGesture命令,你可以在cellForRowAtIndexPath中执行此操作。

希望这有帮助!

答案 1 :(得分:0)

你应该将swipegesture识别器添加到自定义单元类中,并且我认为可以处理该类的滑动。 commitEditingStyle用于编辑像删除一样的单元格,因此您无法使用它来在单元格中浏览图像。

希望这会有所帮助:)

答案 2 :(得分:0)

如果你想做什么,你应该改变:

let cell = self.tableViewFriends.dequeueReusableCellWithIdentifier("CellTableViewFriends") as! CellTableViewFriends;  

cellForRowAtIndexPath(_:)

let cell = self.tableViewFriends.cellForRowAtIndexPath(indexPath)