gpt4 book ai didi

swift - 像以前一样刷新 ViewController

转载 作者:行者123 更新时间:2023-11-30 12:54:51 24 4
gpt4 key购买 nike

我正在使用 Swift 2,但我陷入了一件事。

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

我有一个 SWRevealViewController。

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

谁能帮帮我

点击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 时,您似乎没有重置约束。您可以在 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

关于swift - 像以前一样刷新 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40543704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com