Swift iOS show UITableView edit actions on button tap? - ios

In my custom tableView cell design, there is a "more" button on the right side that is intended to trigger the cell's edit actions. I was wondering if it was possible to show the UITableViewRowActions I've specified in editActionsForRowAt by clicking a button as well as by swiping?
So far I tried:
tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0))?.setEditing(true, animated: true)
But this didn't work.

Related

Swift first cell in tableView lost because add UISearchController

the first cell in table view lost after push navigation from other page. why tableview not scroll to top position.
code
https://github.com/frankenly/ios-tableview-search-lost
https://github.com/frankenly/ios-tableview-search-lost/blob/main/StackOverflow/StackOverflow/ViewController.swift
Firstly you can do one thing
Add search bar above tableview cell as shown in picture
You can add below method
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
Hope it's work for you.Thank you.

How to add a slight delay before triggering didSelectRowAt action and deselecting the tabelview cell

Edit:
I didn't realize that it is the Animates (in segue in storyboard) and animated in pushViewController() which does the job.
How to smooth transition to next view when we tap on a row in table view. I want to achieve similar smooth transitioning effect as Apple's Settings app. Right now my transition happens immediately as soon as row is touched.
On Apple's settings app when you tap on a row on Settings menu, it first highlights the row then smoothly transitions to next view. This is what I want to achieve.
I've searched but can't find any relevant solution. Any help?
Following is my code for row select
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
tableView.deselectRow(at: indexPath, animated: true)
//.....
self.navigationController?.pushViewController(destinationVC, animated: false)
}
You need to change the presentation style. So click on your segue in the storyboard. Then select Kind as Push from the dropdown in Attributes Inspector on the right side.
Also, to present it programmatically, you need to set an identifier in the first text field in Attributes Inspector. After that in your tableView(_:didSelectRowAt:) add this.
performSegue(withIdentifier: "SegueIdentifier", sender: nil)
If you want a delayed action then you can use asyncAfter, like suggested here.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.33) {
self.deselectRow(at: indexPath, animated: true)
self.performSegue(withIdentifier: "SegueIdentifier", sender: nil)
}

Expandable view within tableview

I currently have a table view controller which consist of not only the cells but also a UIView. Now, within that UIView there's a label which might have more than 1 line of text with a See More button. When I pressed that button, the button itself will disappear and the text.numberOfLines is set to 0 so the View should expand to show all text. Which doesn't seems to work in my case, The button does disappear though and the text just continues to the edge of the screen, truncated instead of extending down.
But when this whole UIView is outside the table view controller the functions above work just as expected, but not after I've moved them to within the table View right above the prototype cells. Any Ideas?
When you are setting up the tableviewcell, you have to specify a fixed height for the row with the delegate function
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) {
if expandedArray[indexPath.row] {
return 60
} else {
return UITableViewAutomaticDimension
}
}
Then keep an array of bools, to specify if the cell is expanded or not. Primarily set the bools to false indicating "not expanded".
Then when the use presses the "see more" button, make the bool in the array with the right index true, indicating that the cell is expanded. and call the below code updating the cell.
tableView.beginUpdates()
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
tableView.endUpdates()
That should do the trick.
P.S: Don't forget to properly add constraints inside tableview cell.
Use UITableViewAutomaticDimension and reload your cell on click of see more button.

How to resolve UITableView 'jump' when tapping status bar?

I have a tableViewController with custom cells that each contain a textField. Cells are added to the tableView by clicking an 'Add' button which calls the insertRows(at:with:) method. The cells have a reuseIdentifier and are being reused.
The issue:
Given the user has added a bunch of rows and is editing a textField in one of the bottom rows, when the user tries to scroll to the top by tapping the status bar at the top of the screen, then the tableView jumps / glitches on the first tap, but then scrolls to the top (as expected) after the user taps the status bar a second time.
Note: this jump / glitch doesn't occur when the textField being edited is the first textField to be edited in the tableView, it only occurs from the second edit onwards.
The goal: For the tableView to scroll to the top when the status bar is tapped, without hiding the keyboard.
Things I've tried
Manually setting tableView.scrollsToTop = true
Adding tableView.beginUpdates() before calling insertRows(at:with:), and endUpdates() after
Resigning first responder when textField finished editing
Searching the web for many hours to understand why this is happening, to no avail
#IBAction func addButtonTapped(_ sender: Any) {
let indexPath = IndexPath(row: cells.count, section: 0)
cells.append(cell(text: ""))
tableView.insertRows(at: [indexPath], with: .none)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCellIdentifier", for: indexPath) as! TableViewCell
return cell
}
Attached GIF shows the jump / glitch when the user taps the status bar for the first time, followed by the expected behaviour when the status bar is tapped for the second time.
TableView Jump / Glitch
Any help would be most appreciated. Thanks in advance!

Tableview - Disclosure Indicator

I've set the cell accessory to "disclosure indicator" and I have linked that cell with an ctrl - click ("show (eg. push)") to another view. But when I test the app, the cell just becomes grey and nothing more (look at the screenshot). I want this to behave like a Settings-like view.
any hints?
thx, Tim
The disclosure indicatoris just that; an indicator, not a button, so when you make a segue from a cell , with or without a disclosure indicator, it needs to be a "selection" segue. The "accessory action" section of the choices you get when making the segue should be use if you have a "detail disclosure" button, and you want the segue triggered by touching the button itself.
if you want to navigate by code means try this method.
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
//navigateTo your viewController
}
You can find this in TableView Delegate.

Resources