2016-11-11 114 views
0

我正在使用Swift 2,我陷入了一件事。刷新视图控制器之前是

我有一个TableViewController,我把一个UIView放到单元格中给出一个Swipeable效果,并且还分配了自定义的单元格类。

我有一个SWRevealViewController。

首先我滑过细胞,然后打开RevealView Sider bar菜单。 现在,当我的侧栏菜单单元格不会恢复正常。

谁能帮我请

点击here! 这张图片显示了我所做的滑动效果。

点击here! 现在在这张图片中,您可以清楚地看到当我打开边栏菜单时单元格未关闭。

在我的SwipeCell的Custom类中,我使用约束点来更改UIView的状态。

*import UIKit 
protocol SwipeableCellDelegate: UIGestureRecognizerDelegate 
{ 
    func buttonOneActionForItemText(itemText : Int ,myIndexPath : NSIndexPath) 

    func buttonTwoActionForItemText(itemText : Int,myIndexPath : NSIndexPath) 

    func buttonThreeActionForItemText(itemText : Int,myIndexPath : NSIndexPath) 

} 
class SwipeableCellTableViewCell: UITableViewCell { 

    @IBOutlet weak var myview: UIView! 

    var panRecognizer: UIPanGestureRecognizer! 
    var panStartPoint : CGPoint! 
    var startingRightLayoutConstraintConstant : CGFloat! 

    @IBOutlet weak var contentViewRightConstraint : NSLayoutConstraint! 
    @IBOutlet weak var contentViewLeftConstraint : NSLayoutConstraint! 


    var halfOfButtonOne: CGFloat! 

    @IBOutlet weak var deleteButton: UIButton! 

    @IBOutlet weak var seeButton: UIButton! 

    @IBOutlet weak var confirmButton: UIButton! 

    var delegate: SwipeableCellDelegate? 
    var itemText : Int? 

    let kBounceValue: CGFloat = 20.0 
    var myIndexPath : NSIndexPath! 



    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.panThisCell)) 
     self.panRecognizer.delegate = self 
     self.myview.addGestureRecognizer(self.panRecognizer) 



    } 




    func panThisCell(recognizer: UIPanGestureRecognizer) { 
     switch recognizer.state { 
     case .Began: 
      self.panStartPoint = recognizer.translationInView(self.myview) 
      self.startingRightLayoutConstraintConstant = self.contentViewRightConstraint.constant 
      print("Pan Began at \(NSStringFromCGPoint(self.panStartPoint))") 

     case .Changed: 
      let currentPoint = recognizer.translationInView(self.myview) 
      let deltaX: CGFloat = currentPoint.x - self.panStartPoint.x 
      var panningLeft = false 
      if currentPoint.x < self.panStartPoint.x { 
       //1 
       panningLeft = true 
      } 
      if self.startingRightLayoutConstraintConstant == 0 
      { 
       //2 
       //The cell was closed and is now opening 
       if !panningLeft { 
        let constant: CGFloat = max(-deltaX, 0) 
        //3 
        if constant == 0 { 
         //4 
         self.resetConstraintContstantsToZero(true, notifyDelegateDidClose: false) 
        } 
        else { 
         //5 
         print("changed5: \(constant)") 
         self.contentViewRightConstraint.constant = constant 
        } 
       } 
       else { 
        let constant: CGFloat = min(-deltaX, self.buttonTotalWidth()) 
        //6 
        if constant == self.buttonTotalWidth() { 
         //7 
         self.setConstraintsToShowAllButtons(true, notifyDelegateDidOpen: false) 
        } 
        else { 
         //8 
         print("changed8: \(constant)") 

         self.contentViewRightConstraint.constant = constant 
        } 
       } 
      } 
      else 
      { 
       //The cell was at least partially open. 
       var adjustment: CGFloat = self.startingRightLayoutConstraintConstant - deltaX 
       //1 
       if !panningLeft { 
        var constant: CGFloat = max(adjustment, 0) 
        //2 
        if constant == 0 { 
         //3 
         self.resetConstraintContstantsToZero(true, notifyDelegateDidClose: false) 
        } 
        else { 
         //4 
         print("changed4: \(constant)") 
         self.contentViewRightConstraint.constant = constant 
        } 
       } 
       else { 
        var constant: CGFloat = min(adjustment, self.buttonTotalWidth()) 
        //5 
        if constant == self.buttonTotalWidth() { 
         //6 
         self.setConstraintsToShowAllButtons(true, notifyDelegateDidOpen: false) 
        } 
        else { 
         //7 
         print("changed7: \(constant)") 
         self.contentViewRightConstraint.constant = constant 
        } 
       } 
      } 
      self.contentViewLeftConstraint.constant = -self.contentViewRightConstraint.constant 
      //8 

     case .Ended: 

      if self.startingRightLayoutConstraintConstant == 0 { 
       //1 
       //Cell was opening 
       halfOfButtonOne = CGRectGetWidth(self.deleteButton.frame)/2 
       //2 
       if self.contentViewRightConstraint.constant >= halfOfButtonOne { 
        //3 
        //Open all the way 
        self.setConstraintsToShowAllButtons(true, notifyDelegateDidOpen: true) 
       } 
       else { 
        //Re-close 
        self.resetConstraintContstantsToZero(true, notifyDelegateDidClose: true) 
       } 
      } 
      else if startingRightLayoutConstraintConstant <= halfOfButtonOne 
      { 
       //Cell was closing 
       var buttonOnePlusHalfOfButton2: CGFloat = CGRectGetWidth(self.deleteButton.frame) + (CGRectGetWidth(self.seeButton.frame)/2) 
       //4 
       if self.contentViewRightConstraint.constant >= buttonOnePlusHalfOfButton2 { 
        //5 
        //Re-open all the way 
        self.setConstraintsToShowAllButtons(true, notifyDelegateDidOpen: true) 
       } 
       else { 
        //Close 
        self.resetConstraintContstantsToZero(true, notifyDelegateDidClose: true) 
       } 
      } 
      else 
      { 
       //Cell was closing 
       var buttonOnePlusHalfOfButton3: CGFloat = CGRectGetWidth(self.deleteButton.frame) + CGRectGetWidth(self.seeButton.frame) + (CGRectGetWidth(self.confirmButton.frame)/2) 
       //4 
       if self.contentViewRightConstraint.constant >= buttonOnePlusHalfOfButton3 { 
        //5 
        //Re-open all the way 
        self.setConstraintsToShowAllButtons(true, notifyDelegateDidOpen: true) 
       } 
       else { 
        //Close 
        self.resetConstraintContstantsToZero(true, notifyDelegateDidClose: true) 
       } 
      } 

      print("Pan Ended") 

     case .Cancelled: 

      if self.startingRightLayoutConstraintConstant == 0 { 
       //Cell was closed - reset everything to 0 
       self.resetConstraintContstantsToZero(true, notifyDelegateDidClose: true) 
      } 
      else { 
       //Cell was open - reset to the open state 
       self.setConstraintsToShowAllButtons(true, notifyDelegateDidOpen: true) 
      } 

      print("Pan Cancelled") 

     default: 
      break 
     } 

    } 


    func updateConstraintsIfNeeded(animated: Bool, completion: (finished: Bool) -> Void) { 
     var duration: Float = 0 
     if animated { 
      duration = 0.1 
     } 
     UIView.animateWithDuration(Double(duration), delay: 0, options: .CurveEaseOut, animations: {() -> Void in 
      self.layoutIfNeeded() 
      }, completion: completion) 
    } 


    //Set COnstraint 
    func setConstraintsToShowAllButtons(animated: Bool, notifyDelegateDidOpen notifyDelegate: Bool) 
    { 
     //TODO: Notify delegate. 
     if notifyDelegate { 
      // self.delegate!.cellDidOpen(self) 
     } 



     //1 
     if self.startingRightLayoutConstraintConstant == self.buttonTotalWidth() && self.contentViewRightConstraint.constant == self.buttonTotalWidth() { 
      return 
     } 
     //2 
     self.contentViewLeftConstraint.constant = -self.buttonTotalWidth() - kBounceValue 
     self.contentViewRightConstraint.constant = self.buttonTotalWidth() + kBounceValue 
     self.updateConstraintsIfNeeded(animated, completion: {(finished: Bool) -> Void in 
      //3 
      self.contentViewLeftConstraint.constant = -self.buttonTotalWidth() 
      self.contentViewRightConstraint.constant = self.buttonTotalWidth() //check1 
      self.updateConstraintsIfNeeded(animated, completion: {(finished: Bool) -> Void in 
       //4 
       self.startingRightLayoutConstraintConstant = self.contentViewRightConstraint.constant 
      }) 
     }) 
    } 


    //Reset Constraints 
    func resetConstraintContstantsToZero(animated: Bool, notifyDelegateDidClose notifyDelegate: Bool) 
    { 

     if notifyDelegate { 
      //self.delegate!.cellDidClose(self) 
     } 

     //TODO: Notify delegate. 
//  if self.startingRightLayoutConstraintConstant == 0 && self.contentViewRightConstraint.constant == 0 { 
//   //Already all the way closed, no bounce necessary 
//   return 
//  } 

     self.contentViewRightConstraint.constant = -kBounceValue 
     self.contentViewLeftConstraint.constant = kBounceValue 
     self.updateConstraintsIfNeeded(animated, completion: {(finished: Bool) -> Void in 
      self.contentViewRightConstraint.constant = 0 
      self.contentViewLeftConstraint.constant = 0 
      self.updateConstraintsIfNeeded(animated, completion: {(finished: Bool) -> Void in 
       self.startingRightLayoutConstraintConstant = self.contentViewRightConstraint.constant 
      }) 
     }) 
    } 




    func buttonTotalWidth() -> CGFloat { 
     return CGRectGetWidth(self.frame) - CGRectGetMinX(self.confirmButton.frame) 
    } 
    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 
     // Configure the view for the selected state 
    } 


    @IBAction func buttonClicked(sender: AnyObject) 
    { 
     if sender as! NSObject == self.deleteButton { 
      self.delegate?.buttonOneActionForItemText(itemText! ,myIndexPath: myIndexPath) 
      print("Clicked delete 1!") 
     } 
     else if sender as! NSObject == self.seeButton { 
      self.delegate!.buttonTwoActionForItemText(itemText!,myIndexPath: myIndexPath) 
      print("Clicked see 2!") 
     } 
     else { 
      self.delegate!.buttonThreeActionForItemText(itemText!,myIndexPath: myIndexPath) 
      print("Clicked confirm button!") 
     } 

    } 

} 

TableViewCellForRow:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SwipeableCellTableViewCell 

     let temp = requestedUserInfo[indexPath.row] 

     var username = cell.viewWithTag(100) as! UILabel 
     var profile = cell.viewWithTag(101) as! UIImageView 

     let url = temp.imageUrl[0] 

     //print("uname: \(temp.uName)") 

     username.text = temp.uName 
     profile.kf_setImageWithURL(NSURL(string: url)) 


     cell.myIndexPath = indexPath 
     cell.itemText = indexPath.row 
     cell.delegate = self 


     //  if self.cellsCurrentlyEditing.contains(indexPath) { 
     //   cell.openCell() 
     //  } 


     return cell 
    } 

这是viewDidLoad中代码:

override func viewDidLoad() { 
     super.viewDidLoad() 

     navigationController?.navigationBar.translucent = false 

     if revealViewController() != nil { 
      //revealViewController().rearViewRevealWidth = 62 
      sidebarButton.target = revealViewController() 
      sidebarButton.action = #selector(SWRevealViewController.revealToggle(_:)) 

      revealViewController().rightViewRevealWidth = self.view.frame.width-64 
      rightReveal.target = revealViewController() 
      rightReveal.action = #selector(SWRevealViewController.rightRevealToggle(_:)) 

      view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
      self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer()) 


     } 

     self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh") 
     self.refreshControl?.addTarget(self, action: #selector(FriendRequestTableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged) 

     if NSUserDefaults.standardUserDefaults().valueForKey(KEY_UID) != nil { 
      self.showRequests() 
     } 
    } 
+0

刷新的数据表视图ü没有使用“tableView.reloadData()你的行动后,您可以请告诉我你的代码和用户界面,其中的问题。发生了吗? –

+0

你可以在链接中看到UI。我尝试了tableView.reload数据,但数据正在更新,但单元格处于相同状态(如果单元格被刷新,则重新加载后它将保持相同状态) –

+0

您能向我们展示您的cellForRowAtIndexPath()以及您是如何制作滑动效果的?机会是您在重新启动您的自定义单元格时出现这种情况。 –

回答

0

那么它看起来像你当实现代码如下重新加载不是在重置的约束。你可以在做这样的事情你cellForRowAtIndexPath()

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SwipeableCellTableViewCell 

    ... 

    cell.myIndexPath = indexPath 
    cell.itemText = indexPath.row 
    cell.delegate = self 
    cell.resetConstraintContstantsToZero(true, notifyDelegateDidClose: false) 
    return cell 
} 

您可能要检查你的电池在你的resetConstraintContstantsToZero()功能动画之前已经关闭。

顺便说一句,你可能想看看这个:Swipe-able Table View Cell in iOS 9

+0

先生您可以帮我解释一下,我应该如何检查是否关闭resetConstraintConstantsToZero()函数。 ? –

+0

cell.resetConstraintContstantsToZero(true,notifyDelegateDidClose:false)This works :) :) –

+0

@SaadUllah很高兴我能帮到你。您可以简单地使用if else语句来检查约束是否已经为0,并且如果已经是您不必重新设置约束。 –

相关问题