2017-08-15 56 views
16

正如我的标题所说,我想从第一行UITableView左侧滑动到右侧,当用户来到那个ViewController时。轻扫UITableViewCell无触摸

在我的ViewController我有一个UITableView,每行有两个按钮“More”和“Delete”动作。看下面的代码

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
    } 
} 


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { action, index in 
     // Edit Button Action 
    } 
    deleteButton.backgroundColor = UIColor.red 

    let editButton = UITableViewRowAction(style: .normal, title: "Edit") { action, index in 
     // Delete Button Action 
    } 
    editButton.backgroundColor = UIColor.lightGray 

    return [deleteButton, editButton] 
} 

所有的工作都很好。但我希望当最终用户第一次登录此ViewController时,他们会通知有可用的滑动操作,以便他们执行此操作。

问题是:我怎样才能左右自动刷第一行?

我做了什么?

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    let cell = posTblView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! POSUserTabelCell 
    UIView.animate(withDuration: 0.3, animations: { 
     cell.transform = CGAffineTransform.identity.translatedBy(x: -150, y: 0) 
    }) { (finished) in 
     UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: { 
      cell.transform = CGAffineTransform.identity 
     }, completion: { (finished) in 
     }) 
    } 
} 

通过上面的代码刷卡/移动单元格正在工作,但不显示“删除”和“更多”按钮。

所以请引导我正确的方向。

+0

您的方法似乎适合左右滑动。但是,我不确定内置按钮是否可以像这样突出显示。你可以肯定的做法是在你的内容下面放置一个看起来像本地控件的UIView,做一些滑动操作来展示它们,并且将它们从单元格中移除/隐藏。看看这个答案:https://stackoverflow.com/questions/5301728/is-it-possible-to-programmatically-show-the-red-delete-button-on-a-uitableviewce – dirtydanee

+0

你可以使用这个旧的框架: https://github.com/CEWendel/SWTableViewCell。然后在viewdidappear写: [cell showRightUtilityButtonsAnimated:YES]; –

+0

那么,你的目标是让第一行自动滑动,这样你就可以看到编辑按钮。是对的吗? –

回答

6

我发现实现这一目标的方法之一。

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    if arrStudent.count > 0 { 
     let indexPath = NSIndexPath(row: 0, section: 0) 
     let cell = tblStudent.cellForRow(at: indexPath as IndexPath); 

     let swipeView = UIView() 
     swipeView.frame = CGRect(x: cell!.bounds.size.width, y: 0, width: 170, height: cell!.bounds.size.height) 
     swipeView.backgroundColor = .clear 

     let swipeEditLabel: UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 80, height: cell!.bounds.size.height)) 
     swipeEditLabel.text = "Edit"; 
     swipeEditLabel.textAlignment = .center 
     swipeEditLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeEditLabel.backgroundColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 1) // Light Gray: Change color as you want 
     swipeEditLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeEditLabel) 

     let swipeDeleteLabel: UILabel = UILabel.init(frame: CGRect(x: swipeEditLabel.frame.size.width, y: 0, width: 90, height: cell!.bounds.size.height)) 
     swipeDeleteLabel.text = "Delete"; 
     swipeDeleteLabel.textAlignment = .center 
     swipeDeleteLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeDeleteLabel.backgroundColor = UIColor(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red color: Change color as you want 
     swipeDeleteLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeDeleteLabel) 

     cell!.addSubview(swipeView) 

     UIView.animate(withDuration: 0.60, animations: { 
      cell!.frame = CGRect(x: cell!.frame.origin.x - 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 170, height: cell!.bounds.size.height) 
     }) { (finished) in 
      UIView.animate(withDuration: 0.60, animations: { 
       cell!.frame = CGRect(x: cell!.frame.origin.x + 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 170, height: cell!.bounds.size.height) 

      }, completion: { (finished) in 
       for subview in swipeView.subviews { 
        subview.removeFromSuperview() 
       } 
       swipeView.removeFromSuperview() 
      }) 

     } 
    } 
} 

在电池的末端添加自定义UIView和删除动画完成后。

  • ,只要你想
  • 添加标签(S)与背景颜色和标题,你想,你可以设置自定义视图的框架。

在这里你应该写的代码不是每次调用这个代码在viewDidAppear它应该只调用一次只有用户指示。

+0

我会尝试你的逻辑,希望它能帮助我。 – Johnty

+0

非常感谢你:)我尽我所能,努力工作。感谢agin男士。 – Johnty

+0

@Johnty很高兴帮助你:) – Govaadiyo

1

将单元格转换到左侧不会使操作按钮可见。

您需要通过调用单元上的setEditing方法发送到单元格编辑模式,把这个在您的viewDidAppear

let cell = posTblView.cellForRow(at: IndexPath(row: 0, section: 0)) 
cell.setEditing(true, animated: true) 
+0

试过但没有工作。 – Johnty

相关问题