使用滑动操作更改UIImageView图像



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

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  
}  

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

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中这样做。

希望这能有所帮助!

如果这是您想要做的,您应该更改:

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

到cellForRowAtIndexPath(_:)

let cell = self.tableViewFriends.cellForRowAtIndexPath(indexPath)

您应该向自定义单元格类添加swipegesture recgnozer,并处理该类中的swipe。commitEditingStyle用于编辑类似于删除的单元格,所以您不能使用它来交换单元格中的图像。

希望这将有所帮助:)

最新更新