简体   繁体   English

如何在TAP上而不是滑动上编辑TableView单元格-Swift

[英]How to edit tableview cell on TAP instead of swipe - Swift

I have a UITableViewController and at the moment, the user can swipe left on the cell to reveal an 'add to favourites' button. 我有一个UITableViewController ,此刻,用户可以在单元格上向左滑动以显示“添加到收藏夹”按钮。 I would now like to add an icon to the cell that the user can tap to reveal the this button (does the same thing as swiping to the left). 现在,我想向用户添加一个图标,用户可以点击该图标以显示此按钮(与向左滑动相同)。 This will just act as a visual aid for the users who do not know swiping left will do the same thing. 对于不知道向左滑动的用户来说,这只会起到视觉辅助作用。

For my TableView , I use the following code to 'close the cell' when the user has pressed the button: 对于TableView ,当用户按下按钮时,我使用以下代码“关闭单元格”:

itemsTableView.setEditing(false, animated: true)

From this I assumed that to manually open the cell I would simply call: 由此,我假设要手动打开该单元格,只需调用:

//Get cell
let cell:TableViewCell = itemsTableView.cellForRowAtIndexPath(NSIndexPath(forRow: 
sender.tag, inSection: 0)) as! TableViewCell 

//Open cell to reveal button
cell.setEditing(true, animated: true)

Unfortunately this does not work. 不幸的是,这不起作用。 So I would like to know what is the function that is called when a user swipes to the left of a cell and how can I replicate it when a user taps on the icon? 因此,我想知道当用户在单元格左侧滑动时调用的函数是什么,当用户点击图标时如何复制它?

I don't think you're going to be able to do it, sorry. 对不起,我认为您将无法做到。 This feature relies on the UITableView's pan gesture recognizer. 此功能依赖于UITableView的平移手势识别器。 When the user pans sideways, the table view checks the editActions... delegate method and, if we have edit actions, inserts a private confirmation view behind the cell and allows the cell to be moved to reveal it. 当用户侧向平移时,表视图将检查editActions...委托方法,如果有编辑操作,则在单元格后面插入一个私有确认视图,并允许移动该单元格以显示它。 You can't trigger that behavior programmatically as far as I can tell. 据我所知,您无法以编程方式触发该行为。

This would be a reason — one of many very good reasons — for sticking with a third-party scrollable cell class. 这将是坚持使用第三方可滚动单元类的一个原因(这是非常好的原因之一)。 In this way, the cell is a horizontal scroll view and you just scroll it in code. 这样,该单元格是一个水平滚动视图,您只需在代码中滚动它即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM