【问题标题】:Refresh the ViewController as is before像以前一样刷新 ViewController
【发布时间】:2016-11-11 08:11:19
【问题描述】:

我正在使用 Swift 2,但我被困在一件事上。

我有一个 TableViewController,我将一个 UIView 放在一个单元格中以提供可滑动的效果并分配自定义单元格类。

我有一个 SWRevealViewController。

首先我滑动单元格,其次我打开 RevealView Sider 栏菜单。 现在当我打开侧边栏菜单单元格时不会恢复正常。

谁能帮帮我

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

点击here! 现在在这张图片中你可以清楚地看到当我打开侧边栏菜单时单元格没有关闭

在我的 SwipeCell 自定义类中,我使用约束出口来更改 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()
        }
    }

【问题讨论】:

  • 在您使用“tableView.reloadData() 执行操作后重新加载表格视图中的数据。您能告诉我您的代码和 UI 出现问题的地方吗?
  • 您可以在链接中看到 UI。我尝试了 tableView.reload 数据,但数据正在更新,但单元格处于相同状态(如果单元格向左滑动,则重新加载后它将保持相同状态)
  • 你能告诉我们你的 cellForRowAtIndexPath() 以及你是如何制作滑动效果的吗?很有可能您在出队时没有重置自定义单元格的状态。

标签: swift uiview tableview swrevealviewcontroller


【解决方案1】:

看起来你在重新加载 tableview 时没有重置约束。你可以在你的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

【讨论】:

  • 先生,您能帮我看看如何在 resetConstraintConstantsToZero() 函数中检查是否关闭。 ?
  • cell.resetConstraintContstantsToZero(true, notifyDelegateDidClose: false) 这个工作:)
  • @SaadUllah 很高兴我能帮上忙。您可以简单地使用 if else 语句来检查约束是否已经为 0,如果已经是,则无需再重置约束。
  • 谢谢.....先生你能告诉我当我点击打开侧边栏菜单的 SWRevealController 按钮时我是如何刷新的。
猜你喜欢
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
相关资源
最近更新 更多