我有一个删除按钮,在刷卡时变得可见,但我真的很喜欢邮件应用程序中发生的事情,如果你继续刷卡,将使用删除按钮,删除该项目.
我已经看过很多关于使用实现项目的教程
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
但我无法弄清楚使用什么来使按钮扩展到表格单元格的整个宽度.有什么想法吗?
编辑:我已经决定只在删除键被显示后禁用滑动,我不喜欢你如何刷表格单元格以查看底层背景.
最佳答案 这可以使用UIScrollView分页.
创建一个UIScrollView并将其作为子视图添加到UITableViewCell
(还要添加大小约束以使scrollview的大小与其父边界相同)
现在为scrollview设置此代码:
//set scroll view
scrollview.delegate = self;
scrollview.pagingEnabled = true;
scrollview.showsHorizontalScrollIndicator = false;
scrollview.showsVerticalScrollIndicator = false;
scrollview.contentSize.width = scrollview.bounds.width * 2.0;
//create delete swipe view
let deleteButton = UIView(frame: CGRectMake(scrollview.bounds.width,0,scrollview.bounds.width * 2.0,scrollview.bounds.height))
deleteButton.backgroundColor = UIColor.redColor()
let deleteLabel = UILabel(frame: CGRectMake(0,0,scrollview.bounds.width * 0.3,scrollview.bounds.height))
deleteLabel.textColor = UIColor.whiteColor()
deleteLabel.text = "Delete"
deleteLabel.contentMode = .Center
deleteLabel.textAlignment = .Center
deleteButton.addSubview(deleteLabel)
scrollview.addSubview(deleteButton)
现在委托功能:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView.contentOffset.x == 0{
return;
}
let confirmDelete = UIAlertController(title: "Are you sure you want to delete?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
confirmDelete.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Destructive, handler: { (_) -> Void in
//Delete confirmed
}))
confirmDelete.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
scrollView.contentOffset.x = 0;
}))
rootViewController.presentViewController(confirmDelete, animated: true, completion: nil);
}
这将创建下一个效果: