Temporarily Hiding a cell in UICollectionView Swift iOS - ios

I've been trying this for hours with no luck. I have a UICollectionView collectionView. The collection view is basically a list with the last cell always being a cell with a big plus sign to add another item. I've enabled reordering with the following. What I'd like for it to do is when I start the interactive movement, the plus sign cell goes away, and then when the user is done editing, it appears again. This is a basic version of the code I have:
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.Began:
...
self.collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
removeAddCell()
case UIGestureRecognizerState.Changed:
case UIGestureRecognizerState.Ended:
...
collectionView.endInteractiveMovement()
replaceAddCell()
default:
collectionView.cancelInteractiveMovement()
}
}
func removeAddCell(){
print("Reloading data - removing add cell")
data_source.popLast()
self.collectionView.reloadData()
}
func replaceAddCell(){
print("Reloading data - replacing add cell")
data_source.append("ADD BUTTON")
self.collectionView.reloadData()
}
It's very rough pseudocode, but I can't even get the simplest version of this to work. With the code I have, it gives me the dreaded "Fatal error: unexpectedly found nil while unwrapping an Optional values" on the line where I reference the UICollectionViewCell after removing the items from the data source.
If anyone who has done something like this could share their approach I'd really appreciate it! Thank you!
-Bryce

You can do something like this:
func longPressed(sender: UILongPressGestureRecognizer) {
let indexPath = NSIndexPath(forItem: items.count - 1, inSection: 0)
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! YourCollectionViewCell
switch sender.state {
case .Began:
UIView.animateWithDuration(0.3, animations: {
cell.contentView.alpha = 0
})
case .Ended:
UIView.animateWithDuration(0.3, animations: {
cell.contentView.alpha = 1
})
default: break
}
}
this way it gradually disappears instead of abruptly.

I've done something like this. The data source for the collection view tracks a BOOL to determine whether or not to show the Add Item Cell. And call insertItemsAtIndexPaths: and deleteItemsAtIndexPaths: to animate the Add Item Cell appearing and disappearing. I actually use a Edit button to toggle the modes. But you can adapt this code to use your gesture recognizer.
basic code:
self.editing = !self.editing; // toggle editing mode, BOOL that collection view data source uses
NSIndexPath *indexPath = [self indexPathForAddItemCell];
if (!self.editing) { // editing mode over, show add item cell
if (indexPath) {
[self.collectionView insertItemsAtIndexPaths:#[indexPath]];
}
}
else { // editing mode started, delete add item cell
if (indexPath) {
[self.collectionView deleteItemsAtIndexPaths:#[indexPath]];
}
}

Related

Is it unsafe to call reloadData() after getting an indexPath but before removing a cell at that indexPath?

I'm trying to track down a difficult crash in an app.
I have some code which effectively does this:
if let indexPath = self.tableView.indexPath(for: myTableViewCell) {
// .. update some state to show a different view in the cell ..
self.tableView.reloadData()
// show nice fade out of the cell
self.friendRequests.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
The concern is that calling reloadData() somehow makes the indexPath I just retrieved invalid so the app crashes when it tries to delete the cell at that indexPath. Is that possible?
Edit:
The user interaction is this:
User taps a button [Add Friend] inside of table view cell <-- indexPath retrieved here
Change the button to [Added] to show the tap was received. <-- reloadData called here
Fade the cell out after a short delay (0.5s). <-- delete called here with indexPath from #1
I can change my code to not call reloadData and instead just update the view of the cell. Is that advisable? What could happen if I don't do that?
Personally, I'd just reload the button in question with reloadRows(at:with:), rather than the whole table view. Not only is this more efficient, but it will avoid jarring scrolling of the list if you're not already scrolled to the top of the list.
I'd then defer the deleteRows(at:with:) animation by some small fraction of time. I personally think 0.5 seconds is too long because a user may proceed to tap on another row and they can easily get the a row other than what they intended if they're unlucky enough to tap during the wrong time during the animation. You want the delay just long enough so they get positive confirmation on what they tapped on, but not long enough to yield a confusing UX.
Anyway, you end up with something like:
func didTapAddButton(in cell: FriendCell) {
guard let indexPath = tableView.indexPath(for: cell), friendsToAdd[indexPath.row].state == .notAdded else {
return
}
// change the button
friendsToAdd[indexPath.row].state = .added
tableView.reloadRows(at: [indexPath], with: .none)
// save reference to friend that you added
let addedFriend = friendsToAdd[indexPath.row]
// later, animate the removal of that row
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
if let row = self.friendsToAdd.index(where: { $0 === addedFriend }) {
self.friendsToAdd.remove(at: row)
self.tableView.deleteRows(at: [IndexPath(row: row, section: 0)], with: .fade)
}
}
}
(Note, I used === because I was using a reference type. I'd use == with a value type that conforms to Equatable if dealing with value types. But those are implementation details not relevant to your larger question.)
That yields:
Yes, probably what's happening is the table view is invalidating stored index path.
To test whether or not it is the issue try to change data that is represented in the table right before reloadData() is called.
If it is a problem, then you'll need to use an identifier of an object represented by the table cell instead of index path. Modified code will look like this:
func objectIdentifer(at: IndexPath) -> Identifier? {
...
}
func indexPathForObject(_ identifier: Identifier) -> IndexPath? {
...
}
if
let path = self.tableView.indexPath(for: myTableViewCell)
let identifier = objectIdentifier(at: path) {
...
self.tableView.reloadData()
...
if let newPath = indexPathForObject(identifier) {
self.friendRequests.removeObject(identifier)
self.tableView.deleteRows(at: [newPath], with: .fade)
}
}

how can I refresh a single button in UITableViewCell instead of refreshing whole table or whole cell?

In my swift app I have a UITableView with one static cell and many dynamic cells.
Static cell contains different fields, such as labels, map (taken from MapKit) and a button, that indicates whether user voted up or not.
Now, when user presses the button, I want to change its color, possibly without refreshing anything else.
So far my code looks like this:
var currentUserVote:Int = 0
func tableView(_ tview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath as NSIndexPath).row == 0 {
let cell = tview.dequeueReusableCell(withIdentifier: "cellStatic") as! VideoDetailsCell
fetchScore(cell.score)
let voteUpImage = UIImage(named: "voteUp");
let tintedVoteUpImage = voteUpImage?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
cell.voteUpButton.setImage(tintedVoteUpImage, for: UIControlState())
checkUsersVote() { responseObject in
if(responseObject == 1) {
cell.voteUpButton.tintColor = orangeColor
} else if (responseObject == -1){
cell.voteUpButton.tintColor = greyColor
} else {
cell.voteUpButton.tintColor = greyColor
}
self.currentUserVote = responseObject
}
//map handling:
let regionRadius: CLLocationDistance = 1000
let initialLocation = CLLocation(latitude: latitude, longitude: longitude)
centerMapOnLocation(initialLocation, map: cell.mapView, radius: regionRadius)
//cell.mapView.isScrollEnabled = false
cell.mapView.delegate = self
.
.
.
return cell
} else {
//handle dynamic cells
}
}
So in the method above I'm checking if user voted already and based on that I'm setting different color on the button. I'm also centering the map on a specific point.
Now, since it's a static cell, I connected IBAction outlet to that button:
#IBAction func voteUpButtonAction(_ sender: AnyObject) {
if(currentUserVote == 1) {
self.vote(0)
}else if (currentUserVote == -1){
self.vote(1)
} else {
self.vote(1)
}
}
and the vote method works as follows:
func vote(_ vote: Int){
let indexPath = IndexPath(row: 0, section: 0)
let cell = tview.dequeueReusableCell(withIdentifier: "cellStatic") as! VideoDetailsCell
switch(vote) {
case 1:
cell.voteUpButton.tintColor = orangeColor
case 0:
cell.voteUpButton.tintColor = greyColor
case -1:
cell.voteUpButton.tintColor = greyColor
default:
cell.voteUpButton.tintColor = greyColor
}
tview.beginUpdates()
tview.reloadRows(at: [indexPath], with: .automatic)
tview.endUpdates()
currentUserVote = vote
//sending vote to my backend
}
My problem is, that when user taps the button, he invokes the method vote, then - based on the vote, the button changes color, but immediately after that method cellForRow is called and it changes the color of the button again. Also, it refreshes the map that's inside of it.
What I want to achieve is that when user taps the button, it should immediately change its color and that's it. Map stays untouched and the button is not changed again from cellForRow.
Is there a way of refreshing only that particular button without calling again cellForRow?
First of all, you confuse static and dynamic cells. You can use static cells only in the UITableViewController and you can't use static and dynamic cell at the same time.
I strongly recommend you not to use cell for storing map and button. All elements from the cell will be released after scrolling it beyond the screen.
I can advise you use TableViewHeaderView for this task. In this case you will be able set button and map view as #IBOutlet.
(See more about adding tableview headerView. You can also set it from interface builder.)
Another way is change tableView.contentInset and set your view with map and button as subview to tableView. This method is used when you need create Stretchy Headers.
It should be quite easy, simply do it in your button handler. The sender argument there is the button object that caused the action. When you were connecting it from IB there was a dropdown to select sender type, you may have missed it and the whole thing would have been obvious with UIButton type there.
Simply change your handler like this :
#IBAction func voteUpButtonAction(_ sender: UIButton) {
if(currentUserVote == 1) {
self.vote(0)
}else if (currentUserVote == -1){
self.vote(1)
} else {
self.vote(1)
}
sender.backgroundColor = yourFavouriteColor
}
Another approach would be to create an IBOutlet for your button, since its from a static cell, and then you would be able to reference it from any place in your view controller.
In this call:
func tableView(_ tview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
I see it calls checkUsersVote() which I'm guessing should get the updated value set in the vote() call. Could the problem be that you aren't doing this
currentUserVote = vote
until after reloadRows() is called?

Trouble moving CollectionViewCell to the end

I have a UICollectionView in a UIViewController. I've configured a gesture recognizer to move cells. It works fine for moving a cell to any index except the end. Most aggravatingly, the app doesn't crash when I attempt to move a cell to the end--it just hangs. I can back out of ReorderViewControllerand go back to it. The view reloads normally.
I call this method from viewDidLoad to configure the gesture recognizer:
func configureGestureRecognizer() {
// configure longPressGestureRecognizer
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ReorderViewController.handleLongPressGesture))
longPressGesture.minimumPressDuration = 0.5
longPressGesture.delegate = self
self.collectionView.addGestureRecognizer(longPressGesture)
}
When the UILongPressGestureRecognizer is triggered, its handler is called:
func handleLongPressGesture(gesture: UILongPressGestureRecognizer) {
guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else {
return
}
let selectedCell = collectionView.cellForItem(at: selectedIndexPath)
switch gesture.state {
case .began:
print("began")
editMode = true
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
selectedCell?.isSelected = true
case .changed:
editMode = true
selectedCell?.isSelected = true
print("changed")
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: self.collectionView))
case .ended:
print("ended")
editMode = false
selectedCell?.isSelected = false
collectionView.endInteractiveMovement()
default:
print("default")
editMode = false
selectedCell?.isSelected = false
collectionView.cancelInteractiveMovement()
}
}
I can move cells with the gesture without any trouble so long as I'm not moving one to the end. Most annoyingly, the app doesn't crash--it just hangs. I can press the "Back" button on the NavBar and go to the prior ViewController without crashing and return to ReorderViewController.
Here's my code for moving cells:
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let stuffToReorder = currentRoutine?.myOrderedSet.mutableCopy() as! NSMutableOrderedSet
stuffToReorder.exchangeObject(at: sourceIndexPath.row, withObjectAt: destinationIndexPath.row)
currentRoutine?.myOrderedSet = stuffToReorder as NSOrderedSet
appDelegate.saveContext()
}
Any thoughts re: where my mistake is are greatly appreciated.
I think I've cracked it. My hunch about CoreData being the issue was a red herring (which is just as well as I don't have much experience of it!). The hang up was caused by the guard statement at the start of your handler method. Specifically, your method checks that there is a valid index path related to the gesture location; if the gesture moves out of the collection view, I think everything gets confused and therefore you get the hang (rather than a crash) as the function keeps exiting at that point. Moving things around a bit, however, seems to solve the problem:
func handleLongPressGesture(gesture: UILongPressGestureRecognizer) {
guard let _ = collectionVC.collectionView else { return }
switch gesture.state {
case .began:
guard let selectedIndexPath = collectionVC.collectionView!.indexPathForItem(at: gesture.location(in: collectionVC.collectionView)) else { return }
selectedCell = collectionVC.collectionView!.cellForItem(at: selectedIndexPath)
print("began")
lastGoodLocation = gesture.location(in: collectionVC.collectionView!)
collectionVC.collectionView!.beginInteractiveMovementForItem(at: selectedIndexPath)
selectedCell.isSelected = true
case .changed:
selectedCell?.isSelected = true
if collectionVC.collectionView!.frame.contains(gesture.location(in: view)) {
print(gesture.location(in: view))
print(collectionVC.collectionView!.frame)
print("INSIDE COLLECTION VIEW!")
collectionVC.collectionView!.updateInteractiveMovementTargetPosition(gesture.location(in: collectionVC.collectionView!))
lastGoodLocation = gesture.location(in: collectionVC.collectionView!)
}
else
{
print("OUTSIDE COLLECTION VIEW!")
collectionVC.collectionView!.updateInteractiveMovementTargetPosition(lastGoodLocation) // Not sure this is needed
}
print("changed")
case .ended:
print("ended")
selectedCell?.isSelected = false
collectionVC.collectionView!.endInteractiveMovement()
default:
print("default")
selectedCell?.isSelected = false
collectionVC.collectionView!.cancelInteractiveMovement()
}
}
Implementing things this way, I moved the guard statement for selectedCell into the .began case of your switch, as this is the only place that it is initialised. I therefore had to declare selectedCell as a class property so that it could be referenced within the other cases later on. I also introduced a CGPoint variable, lastGoodLocation, which stores the last location for which a valid index path is available - this way, if the gesture ends outside the collection view, the cell is sent to that index path.
Anyway, this is a bit rough but certainly seems to prevent the hang. Hope that helps!

Automatic scroll to the next cell in a PFQueryTableViewController in Swift

I have a PFTableViewController with PFTableViewCells in Swift.
In each TableViewCell (with a height of 80% the screen size), there is a button. I would like that when a user select the button, there is an automatic scroll to the next TableViewCell.
Any idea how I can make it?
Thanks a lot
Just determine the indexPath and scroll to the next indexPath. This example assumes a flat table without sections.
func buttonTapped(sender: UIButton) {
let point = sender.convertPoint(CGPointZero, toView:tableView)
let indexPath = tableView.indexPathForRowAtPoint(point)!
// check for last item in table
if indexPath.row < tableView.numberOfRowsInSection(indexPath.section) {
let newIndexPath = NSIndexPath(forRow:indexPath.row + 1 inSection:indexPath.section)
tableView.scrollToRowAtIndexPath(
newIndexPath, atScrollPosition:.Top, animated: true)
}
}

Dynamically scroll through tableView with scrollToRowAtIndexPath

I'd like to visually scroll through my whole tableView. I tried the following, but it doesn't seem to perform the scrolling. Instead it just runs through the loops. I inserted a dispatch_async(dispatch_get_main_queue()) statement, thinking that that would ensure the view is refreshed before proceeding, but no luck.
What am I doing wrong?
func scrollThroughTable() {
for sectionNum in 0..<tableView.numberOfSections() {
for rowNum in 0..<tableView.numberOfRowsInSection(sectionNum) {
let indexPath = NSIndexPath(forRow: rowNum, inSection: sectionNum)
var cellTemp = self.tableView.cellForRowAtIndexPath(indexPath)
if cellTemp == nil {
dispatch_async(dispatch_get_main_queue()) {
self.tableView.scrollToRowAtIndexPath(indexPath!, atScrollPosition: .Top, animated: true)
self.tableView.reloadData()
}
}
}
}
}
I found a solution. Simply use scrollToRowAtIndexPath() with animation. To do so I had to create a getIndexPath() function to figure out where I want to scroll. Has more or less the same effect as scrolling through the whole table if I pass it the last element of my tableView.
If you want it to happen slower with more scrolling effect, wrap it inside UIView.animateWithDuration() and play with 'duration'. You can even do more animation if you want in its completion block. (No need to set an unreliable sleep timer, etc.)
func animateReminderInserted(toDoItem: ReminderWrapper) {
if let definiteIndexPath = indexPathDelegate.getIndexPath(toDoItem) {
self.tableView.scrollToRowAtIndexPath(definiteIndexPath, atScrollPosition: .Middle, animated: true)
}
}

Resources