简体   繁体   中英

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. 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:

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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