Swift first cell in tableView lost because add UISearchController - ios

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.

Related

UITableView ScrollToRow function animating weirdly

The ScrollToRow is not working as I want.
The function I wrote seems to be working fine, but I want it to scroll down to last row only when its added like it happens in chat. When you get a new message, your tableview scrolls to the bottom to read new message.
However, my tableview seems to reload every time before performing scrollToLastRow function (what I mean is every time before scrolling to bottom... it first goes back to top first cell and starts scrolling to bottom from there Every time when a new cell is created).
My code is simple for the scroll:
func scrollToLastRow()
{
let indexPath = IndexPath(row: messages.count - 1, section: 0)
self.chat.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
To check my code:
here's a link to my repository
ChatApplication
the ViewController's used for chat is ---> chatVC
the ViewController used to make chat is -> chatCell
I have tried anything I could get all over I could find.
Tried dispatch (without it the scroll function wasn't working at all)
Used the function on many different places (including viewDidAppear)
Used dispatch.asyncAfter for delay.....
but nothing worked.
#Fahad, you are reloading the tableview before the scrollToLast function is getting called. Please check and please verify at your end.
Note please update your GitHub repository url in your question properly as it not clickable.
Use this function for scrolling to the bottom.
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
use tableview.scrollToBottom()
Use tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation) for adding object to end of the tableView.
From the problem you say and looking in to your code, the animation issues are due to reloading the entire tableView after a new item is added to dataSource array,
What you have to do is to use Call tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation) with the correct indexPath on your tableView and the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) will be invoked.

Why does Collection View load center carousel with this code?

I've been trying to figure out how to make my collection view of 3 cells load with the 2nd cell, and I finally figured it out after looking through StackOverFlow. However, the code that I came across is a bit confusing to me. Would anyone be able to explain why this code below works in making my collection view cell (that covers the whole screen) start with the 2nd of 3 cells? (this is the effect I wanted to achieve all along, but I want to learn more about why this code works exactly.
In this block of code, there's a bool variable and an if statement, why are they needed? When I took out the boolean variable and if statement, the collection view was unable to scroll.
How does this block of code work exactly?
Thank you.
var onceOnly = false
internal override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !onceOnly {
let indexToScrollTo = IndexPath(item: 1, section: 0)
self.collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
onceOnly = true
}
}
1- This scrollToItem in Docs
let indexToScrollTo = IndexPath(item: 1, section: 0)
self.collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
makes the collectionView scroll to the second item in the list
2-
When I took out the boolean variable and if statement, the collection view was unable to scroll
Because willDisplay is called for every cell display , so when for example you scroll to 3rd cell willdisplay is called and causes the collectionView to go to the second cell , so it makes it stuck in the second item all the time ( and this seems like no scroll but the scroll happens and you won't notice that as it happens instantly ) , so the boolean var is needed to make that scroll action happens once which is the scroll to the specified index

UITableView not scrollable after scrollToRow

I have a table with number of rows. I would like to pre-scroll table after it's load and it's not so hard, but I have a problem. Table become non-scrollable after scrollToRow execution.
Here is my code scroll function:
func scrollToValue() {
var scrollPosition: Int = 30
let indexPath = IndexPath(row: scrollPosition, section: 0)
tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
}
and I execute it in viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
scrollToValue()
}
Here is what I have executed this code and try to move table top or bottom: table moves back to 15.0 value at the middle after my interaction, but I would like to scroll to any other position top and bottom without rollback. Even though if I choose false for animation scrollToRow(at: indexPath, at: .middle, animated: false) I can't move cells at all. I can see scroll indicator at the right but cells are frozen.
Remove scroll action from layoutSubviews as tableView is still not loaded and try this
override func viewDidAppear() {
scrollToValue()
}

Cant scroll to invisible item in nested UICollectionViewCell

I'm new in IOS development and I'm developing an app that uses a tableview. Within the tableviewrow I have a UICollectionView to display items in both directions(horizontal and vertical). The problem comes when I try to scroll to the last item in UICollectionView which is non visible and I want to give this item the focus and set it visible.
** cell = UITableViewCell
** tableCell = UICollectionView
I have tried this but does not work:
cell.tableCell.scrollToItem(at: IndexPath, at:.bottom, animated: true)
Please somebody help me!!
Thank you
The 2nd parameter for the scrollToItem function is not of type IndexPath.
open func scrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionViewScrollPosition, animated: Bool)
The type is UICollectionViewScrollPosition. So, the code sould be something like:
cell.tableCell.scrollToItem(at: IndexPath, at: UICollectionViewScrollPosition.bottom, animated: true)

Swift iOS show UITableView edit actions on button tap?

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.

Resources