快速显示更多按钮时取消在表格视图单元格上向左滑动
Posted
技术标签:
【中文标题】快速显示更多按钮时取消在表格视图单元格上向左滑动【英文标题】:Cancel swipe left on tableview cell when showing more buttons in swift 【发布时间】:2016-05-28 03:32:08 【问题描述】:我想知道是否有适当的方法可以取消在 tableview 单元格上向左滑动,以便它向后滑动以隐藏按钮。我不确定如何正确地说,大声笑。但是请看下面的GIF。在第一个 GIF 中,按下取消按钮后我没有代码,并且按钮保持可见。
我唯一的想法是使用此代码重新加载单元格self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
但这让按钮看起来向上移动,而我更希望它看起来单元格向右移动到位。请参阅下面的重新加载 GIF。
我应该如何正确地做到这一点?有关添加按钮的代码及其功能,请参见下文。
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
let edit = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit")
(action, indexPath) in
if (indexPath == self.indexSelect)
print("Editting Selected Cell")
else
print("Editting a difference cell than selected")
let section: Sections = self.frc.objectAtIndexPath(indexPath) as! Sections
let count: Int = self.sectionCount(section.section!)
var msg: String?
let sectionName: String = section.section!
if (count > 0)
msg = "There are \(count) other Items using this Section currently. Editing the Section name \"\(sectionName)\" will affect them all. \n\nThis will be changed immediately!"
let alert = UIAlertController(title: "Edit Section Name", message: msg, preferredStyle: UIAlertControllerStyle.Alert)
let editAction = UIAlertAction(title: "Edit", style: UIAlertActionStyle.Destructive)
UIAlertAction in
let sectionName = Util.trimSpaces(alert.textFields![0].text!)
if (sectionName != section.section)
if (Util.checkSectionName(sectionName, moc: self.moc!) == false)
let entityDesc = NSEntityDescription.entityForName("Sections", inManagedObjectContext: self.moc!)
let newSection: Sections = Sections(entity: entityDesc!, insertIntoManagedObjectContext: self.moc)
newSection.section = sectionName
do
try self.moc!.save()
catch
fatalError("New item save failed")
let oldSection: Sections = section
let fetchReq = NSFetchRequest(entityName: "Catalog")
let pred = NSPredicate(format: "sections.section == %@", oldSection.section!)
fetchReq.predicate = pred
do
let results = try self.moc!.executeFetchRequest(fetchReq)
for rec in results
let catalog: Catalog = rec as! Catalog
catalog.sections = newSection
do
try self.moc!.save()
catch
fatalError("Failed to Save after Delete")
catch
fatalError("Fetching Items to delete section failed")
self.moc!.deleteObject(oldSection)
do
try self.moc!.save()
catch
fatalError("Failed to Save after Delete")
else
Util.msgAlert("Duplicate Section Name", msg: "\"\(sectionName)\" section name already exists.", curVC: self)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
else
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
UIAlertAction in
//self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
alert.addAction(editAction)
alert.addAction(cancel)
alert.addTextFieldWithConfigurationHandler
(txtFld) -> Void in
txtFld.text = section.section
txtFld.autocapitalizationType = UITextAutocapitalizationType.Words
txtFld.autocorrectionType = UITextAutocorrectionType.Default
txtFld.clearButtonMode = UITextFieldViewMode.WhileEditing
self.presentViewController(alert, animated: true, completion: nil)
edit.backgroundColor = UIColor.init(red: 84/255, green: 200/255, blue: 214/255, alpha: 1)
let delete = UITableViewRowAction(style: .Destructive, title: "Delete")
(action, indexPath) in
let section: Sections = self.frc.objectAtIndexPath(indexPath) as! Sections
let count: Int = self.sectionCount(section.section!)
if (count > 0)
let alert = UIAlertController(title: "Confirm Delete", message: "There are \(count) Items using this Section currently. Deleting this Section will reset them all to blank. \n\nThis can't be undone and will take affect immediately!", preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) UIAlertAction in
let deleteAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Destructive) UIAlertAction in
var blankSection: Sections?
var fetchReq = NSFetchRequest(entityName: "Sections")
var pred = NSPredicate(format: "section == %@", "")
fetchReq.predicate = pred
do
let results = try self.moc!.executeFetchRequest(fetchReq)
blankSection = (results.first as! Sections)
catch
fatalError("Fetching blank section failed")
fetchReq = NSFetchRequest(entityName: "Catalog")
pred = NSPredicate(format: "sections.section == %@", section.section!)
fetchReq.predicate = pred
do
let group = try self.moc!.executeFetchRequest(fetchReq)
for rec in group
let catalog: Catalog = rec as! Catalog
catalog.sections = blankSection
catch
fatalError("Fetching Items to delete section failed")
self.moc!.deleteObject(section)
do
try self.moc!.save()
catch
fatalError("Failed to Save after Delete")
if (self.sectionUpdateProtocol != nil)
self.sectionUpdateProtocol!.sectionUpdate(self, section: blankSection!)
//self.navigationController!.popViewControllerAnimated(true)
alert.addAction(deleteAction)
alert.addAction(cancel)
self.presentViewController(alert, animated: true, completion: nil)
else
self.moc!.deleteObject(section)
do
try self.moc!.save()
catch
fatalError("Failed to Save after Delete")
return [delete, edit]
【问题讨论】:
看看 SWTableViewCell 或者看看他们是如何实现的。 github.com/CEWendel/SWTableViewCell 【参考方案1】:只需调用:
tableView.setEditing(false, animated: true)
【讨论】:
就在鼻子上,这就是我想要的。谢谢! 删除按钮需要一段时间才能消失。有没有办法让它立即消失?我什至尝试在解除警报的完成处理程序中执行上述操作,但这也不起作用。我什至将动画设置为 FALSE,也失败了 @zulkarnainshah 我知道这已经晚了,但你可以试试tableView.reloadData()
(设置动画:false,不过应该可以)以上是关于快速显示更多按钮时取消在表格视图单元格上向左滑动的主要内容,如果未能解决你的问题,请参考以下文章
向左滑动时未剪切子视图以删除 UITableViewCell
在uitableview单元格上留下滑动删除按钮打开然后返回导致崩溃
以编程方式在 UITableViewCell 上显示删除按钮