UITableView以编程方式调用滑动操作

我有一个UITableView用户可以向左滑动显示操作(如在iOS 8邮件)。 这一切都按预期工作。 当用户点击单元格的某个部分时,我想要触发这个事件。 我怎样才能以编程方式调用此幻灯片操作?

当前行为:用户必须向左滑动单元格以显示操作button。

期望的行为:用户点击单元上的一个动作button。 单元格滑过以显示操作button。

那么我无法find一种方法来做到这一点编程,但我想出了这个解决方法。 当用户点击单元格时,我将其animation(平移)到左侧以便瞬时显示一个“点击我”button。 这很快就会逆转,细胞恢复正常。 这提供了一个视觉提示,让用户知道他们可以刷单元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; __block UILabel *swipeLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.bounds.size.width, 0, 200, cell.bounds.size.height)]; swipeLabel.text = @" Swipe Me"; swipeLabel.backgroundColor = [UIColor greenColor]; swipeLabel.textColor = [UIColor whiteColor]; [cell addSubview:swipeLabel]; [UIView animateWithDuration:0.3 animations:^{ [cell setFrame:CGRectMake(cell.frame.origin.x - 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.3 animations:^{ [cell setFrame:CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)]; } completion:^(BOOL finished) { [swipeLabel removeFromSuperview]; swipeLabel = nil; }]; }]; } 

希望这有助于某人。

请注意,您需要将您的tableViewCell的selecttypes设置为none。 否则,灰色条将遮掩它。

更新。 我以为我会发布更多的Swifty版本:

 func previewActions(forCellAt indexPath: IndexPath) { guard let cell = tableView.cellForRow(at: indexPath) else { return } let label: UILabel = { let label = UILabel(frame: CGRect.zero) label.text = " Swipe Me " label.backgroundColor = .blue label.textColor = .white return label }() // Figure out the best width and update label.frame let bestSize = label.sizeThatFits(label.frame.size) label.frame = CGRect(x: cell.bounds.width - bestSize.width, y: 0, width: bestSize.width, height: cell.bounds.height) cell.insertSubview(label, belowSubview: cell.contentView) UIView.animate(withDuration: 0.3, animations: { cell.transform = CGAffineTransform.identity.translatedBy(x: -label.bounds.width, y: 0) label.transform = CGAffineTransform.identity.translatedBy(x: label.bounds.width, y: 0) }) { (finished) in UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: { cell.transform = CGAffineTransform.identity label.transform = CGAffineTransform.identity }, completion: { (finished) in label.removeFromSuperview() }) } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { previewActions(forCellAt: indexPath) return } 

对于任何人在寻找Swift版本的VaporwareWolf的答案,这里是:

 func animateRevealHideActionForRow(tableView: UITableView, indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath); // Should be used in a block var swipeLabel: UILabel? = UILabel.init(frame: CGRectMake(cell!.bounds.size.width, 0, 200, cell!.bounds.size.height)) swipeLabel!.text = " Swipe Me"; swipeLabel!.backgroundColor = UIColor.init(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red swipeLabel!.textColor = UIColor.whiteColor() cell!.addSubview(swipeLabel!) UIView.animateWithDuration(0.3, animations: { cell!.frame = CGRectMake(cell!.frame.origin.x - 100, cell!.frame.origin.y, cell!.bounds.size.width + 100, cell!.bounds.size.height) }) { (finished) in UIView.animateWithDuration(0.3, animations: { cell!.frame = CGRectMake(cell!.frame.origin.x + 100, cell!.frame.origin.y, cell!.bounds.size.width - 100, cell!.bounds.size.height) }, completion: { (finished) in swipeLabel?.removeFromSuperview() swipeLabel = nil; }) } } 

斯威夫特3相当于布拉克的回答。 你可以用编程的方式把这个调用放在任何你想要的地方,我把它放在一个辅助函数中,这样任何表视图都可以显示出来。 当用户第一次使用我的应用程序时,我打开它(我感觉需要更长的animation时间)。

 class func animateRevealHideActionForRow(tableView: UITableView, indexPath: NSIndexPath) { let cell = tableView.cellForRow(at: indexPath as IndexPath); // Should be used in a block var swipeLabel: UILabel? = UILabel.init(frame: CGRect.init(x: cell!.bounds.size.width, y: 0, width: 200, height: cell!.bounds.size.height)) swipeLabel!.text = " Swipe Me"; swipeLabel!.backgroundColor = UIColor.red swipeLabel!.textColor = UIColor.white cell!.addSubview(swipeLabel!) UIView.animate(withDuration: 1.0, animations: { cell!.frame = CGRect.init(x: cell!.frame.origin.x - 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 100, height: cell!.bounds.size.height) }) { (finished) in UIView.animate(withDuration: 1.0, animations: { cell!.frame = CGRect.init(x: cell!.frame.origin.x + 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 100, height: cell!.bounds.size.height) }, completion: { (finished) in swipeLabel?.removeFromSuperview() swipeLabel = nil; }) } } 

而对于你想为每一行调用这个函数的Swift 3版本。

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath); UIView.animate(withDuration: 0.3, animations: { cell!.frame = CGRect(x: cell!.frame.origin.x - 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 100, height: cell!.bounds.size.height) }) { (finished) in UIView.animate(withDuration: 0.3, animations: { cell!.frame = CGRect(x: cell!.frame.origin.x + 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 100, height: cell!.bounds.size.height) }, completion: { (finished) in }) } }