I have a custom table view cell that has a toggle button on the left side.
When the tableview's isEditing property is set to true, the delete editing option appears properly, but when it's tapped, nothing happens.
The stranger thing is, when I tap and hold, and then drag to one side away from the button, and THEN lift, the cell finally slides over. Which isn't how it's supposed to work at all.
Even when I remove connections to the toggle button from the storyboard and the subclass, it still behaves this way.
Why is it doing this?
First of all, correct me if I'm wrong, but I think these are two different problems.
1.Nothing happens when the "delete editing" button is tapped. I would check if the "delete editing" button properly connected to the class file with an IBAction.
2.The cell slide behavior is not what is desired. I would check to see if the gesture you're recognizing is a tap and not a left swipe.
Hope that helps.
you need to add following methods in your table view controller
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
//remove element from your array providing index.row
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
)
}
}
Related
I'm trying to implement two cells, one that can be deleted by swiping, and one that can't be deleted, but has swipe actions the other way.
This is what it looks like now:
I don't want the middle cell to be indented when clicking Edit.
Without any custom code, all cells would show this red circle and get a delete-button, but I have added a few lines of code to prevent that.
This code will set all cells without a leading or trailing swipe action to not become editable, which explains the bottom cell in the gif. I have to include the leading swipe actions to this, because if the cell isn't editable, then I can't swipe at all.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
let vm = sections[indexPath.section].viewModels[indexPath.row]
return vm.leadingSwipeActions != nil || vm.trailingSwipeActions != nil
}
The following code will prevent the middle cell from showing the red circle.
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return sections[indexPath.section].viewModels[indexPath.row].trailingSwipeActions == nil ? .none : .delete
}
If I don't pass .none for the cell with leading actions, then it would show the red circle AND it would even show a default deletion-swipe. I don't want the ability to delete this row!
How can I prevent the middle cell from indenting, while still retaining the ability to swipe it?
This method seems to work! tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614873-tableview?language=objc
I have a UITableView which has a swipe to delete action using:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
This works perfectly fine, however there are some situation where the delete should not happen, in this situation when swiping to delete and pressing the delete button the button doesn't disappear but stays there as if nothing is happening.
It seems to me the delete button only collapses when the row is removed from the table (or its backing store).
Is there a way to tell the view to cancel the action?
Add the logic for determining if the row is eligible for editing to
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool (from UITableViewDataSource)
Of course this prevents all editing of the cell not just deleting, this may not be the way to go if you want to allow editing but not deleting.
I'm getting weird behaviour in my TableView
I turned isUserInteractionEnabled on one of my section headers because I need to put a collectionView in it. Before I did anything I noticed that when I tap on that header view (it is a UITableViewCell) it triggers the didSelectRowAt method with the index of a first cell in that section (right below the header). Does anybody know what causes that behaviour and how to turn it off?
Your header sections are part of your UITableView.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
}
In this method indexPath return rows and section, indexPath.section
-> returns section of UitableView, you have tapped and indexPath.row -> returns down of that particular section, tapped.
So this function is going to get called no matter what you do. You
need to override the table view's gesture to avoid this.
How can I make a tableviewcell's slide to delete look like the slide to delete for iOS notifications (fade in and don't touch edge of the screen). I will only have the delete button so I don't need multiple buttons. I would like it to delete upon a full swipe just like the notifications.
Here's a photo of the wanted result with 2 buttons (I only want 1):
The current code I have written only sets the editing style to delete. I have tried using UIContextualAction but I believe I can only set the style, background color, and/or image.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("delete")
}
}
This is what it looks like with my current code:
Try adding a UIView inside the UITableViewCell, with a red background, and a label inside the UIView with the text of delete. Good Luck!
I am working on xamarin.ios. I have a UITableView control at my view. I select a row in it and then move to next screen. If I come back to UITableView screen again the selected row doesn't highlight. It highlight for a second and then deselect automatically. How it can be managed if I come back to the tableview, the selected row should be highlighted.
Hard to say without seeing any code, but looks like the tableview is being reloaded in viewDidAppear. You may want to store the selected row index in NSUserDefaults or somewhere else to persist the selection between view loads/appearances.
Make sure you are setting it selected after the tableview reloads as well, using an appropriate delegate method. Again, without any code to look at, it's hard to see where - and which order - you're doing this, but an example (Swift):
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == tableView.numberOfRowsInSection(0) - 1 {
cell.selected = true
}
}
Alternatively, you could set the selected property in your datasource directly, and then you could do:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
datasource[indexPath.row].selected == true {
cell.selected = true
}
}
If you do it this way, then the selected row will always be set correctly every time the tableview is loaded.
(Full disclosure, I'm not a Xamarin dev, so I'm not sure how those translate from Swift/Obj-C to Xamarin)