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)
Related
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)
}
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
How can I achieve that the images which comes into view is automatically selected?
I know that I can have the first item in the collection view selected by default, but how to do it with every item that comes into view?
To explain it in more detail: I have an imageview depending on the selection of the collectionView. What I want to do is, that the user don't have to select the collectionViewcell to get the depending imageview. I want the user to just swipe through the collection view and the image that comes up is selected and an overview shown in the depending imageview.
The main problem is in my opinion, that I need to Ave just one selected at a time.
You can try to enable multiple selection:
collectionView.allowsMultipleSelection = true
And then in a loop select items that you want to have selected:
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .top)
EDIT
You can use willDisplay delegate method of the UICollectionViewDelegate to do the selection when the cell is being displayed:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// this is called before the cell is displayed, check if this is the cell to be selected, and if yes, select it:
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .top)
}
As far as I understood your question try this selecting an item in the cellForItemAt indexPath. So as soon as the cell is loaded (shown) you select it.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
[...]
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .top)
[...]
}
Also as mentioned in other answer: collectionView.allowsMultipleSelection = true
I think you should use collectionView:willDisplayCell:forItemAtIndexPath: rather than the cellForItemAtIndexPath: (mentioned above) to call the selectItem(at:animated:scrollPosition:), this will ensure the cell is actually about to show.
I am implementing a search feature where you can select results and I'm trying to make it so that it remembers which cells were selected if they show up in the results again.
I'm using cell.setSelected(true, animated: false) in the willDisplayCell function. However, now there is no way to deselect the cells. When tapped on again, neither the didSelectRowAtIndexPath nor the didDeselectRowAtIndexPath functions are being called. What can I do?
Try to use tableView.selectRowAtIndexPath(indexPath: NSIndexPath?NSIndexPath?, animated: Bool, scrollPosition: UITableViewScrollPosition) to select cells. Then you'll get deselect events.
I have a static UITableView with several sections. In one table row I have a UIDatePicker. On touch the table cell expands and I can select the date. Fine so far. But if the table row is on the bottom of the page I need to manually scroll up to select a date. How can I ensure the datepicker to be in view like the calendar app does? Can you please point me into the right direction?
You can use this function:
func scrollToRowAtIndexPath(indexPath: NSIndexPath, atScrollPosition scrollPosition: UITableViewScrollPosition, animated: Bool)
Use it in
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// ...
var indexPathToJump = NSIndexPath(forRow: 0, inSection: 5)
tableView .scrollToRowAtIndexPath( indexPathToJump, atScrollPosition: .None, animated: true)
}
}
Use scrollToRowAtIndexPath:atScrollPosition:animated::
self.tableView.scrollToRowAtIndexPath(myIndexPath, scrollPosition: .None, animated: true)
I haven't tested this syntax, please let me know if it needs improved, but I know that the method is right. You want to use UITableViewScrollPosition.None so that it move the table view just enough that the row in question is in view:
UITableViewScrollPositionNone
The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by UITableViewScrollPositionTop. This is the default.
Available in iOS 2.0 and later.