Reload tableview rows not working unless I scroll - swift

I am currently using a folding cell library and when i close the cell, the labels(dates) and images(workout type) should reload.
tableView.beginUpdates()
UIView.performWithoutAnimation {
tableView.reloadRows(at: indexSet, with: .none)
}
tableView.endUpdates()
I have tried putting my reloadRows in a dispatchQueue.main.async call and everything else that is similar to mine question on stack overflow.

did you try self.tableView.reloadData()?

This type of problem normally occurs when you have not reloaded your tableViews , i.e your Data Source method cellForRowAtIndexPath is not populate tableview elements .
so , try with reload tableView like below
self.tableView.reloadData()

Related

how to show the middle cell of a collection view when my app starts

I have a collectionView embedded in a subview of the ViewController. The collectionView has 12 cells. Each cell takes up the whole width and height of the collection view, so that I can achieve the pagination affect. However, when the app starts, I want to show the middle cell like the 6th or 7th one of my collectionView.
P.S. I have the collectionView in a wrapper view, not in my viewController.
In my WrapperView, I added the following method but as this method is called after the collectionView is added, it shows a sudden jump.
override func didAddSubview(_ subview: UIView) {
super.didAddSubview(subview)
let indexPath = IndexPath(row: 6, section: 0)
DispatchQueue.main.async {
self.calendarCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
}
}
If I could do it before the collection view appear on the screen, I may be able to fix that problem, but I can't find which method is called before the didAddSubview(_:) method in UIView Life Cycle.
Can anyone give me hint on how to solve this.
After your data source has loaded use:
func selectItem(at indexPath: IndexPath?,
animated: Bool,
scrollPosition: UICollectionView.ScrollPosition)
https://developer.apple.com/documentation/uikit/uicollectionview/1618057-selectitem
Probably best used before the view appears so trigger in viewWillAppear or viewDidLoad

Update indexPath value after deleting a row

I have a list of favorite movies. In the table cell, there is a button to delete the movie from the favorites list. So I want to animate it with tableView.deleteRows. But if I delete a row from the top of the screen the other rows coming upward. It is normal but when I was deleting the row from the top of the screen other cells that already shown in the list are not updating indexPath. I assign an asyncAfter method for tableView.reloadData to solve the problem but I think that can cause a crash on the OS side. Because I forced to main thread reload data after a delay. Is it really a problem for OS and what should I do?
Example problem:
If I delete the movie which has the 0 index all of the movies coming upward. So before deletion, 1 index movie should has 0 index but it not. If I tried to delete a new 0 index movie it is deleting another movie.
Work example without tableView.reloadData()
Here is my code // This is working but I think asyncAfter is a problem
cell.deleteButtonActionBlock = {
FavouritesHandler.shared.deleteMovie(movie)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.tableView.reloadData()
}
}
You must not use the captured index path if cells can be deleted, inserted or moved.
In the custom cell declare the closure
var deleteButtonActionBlock : ((UITableViewCell) -> Void)?
and call it
deleteButtonActionBlock?(self)
In cellForRow get the actual index path for the cell
cell.deleteButtonActionBlock = { aCell in
let actualIndexPath = tableView.indexPath(for: aCell)!
FavouritesHandler.shared.deleteMovie(movie)
tableView.deleteRows(at: [actualIndexPath], with: .automatic)
}
No (ugly) delay and no (pointless) reloading needed.
Please note also that I removed the self in the delete line because the tableView instance is available as method parameter.

Swift UITableView scrollToRow not working

I have a tableView with multiple lines and textFields. Some of the textFields won't be visible when the keyboard is visible. So I want the selected row to scroll to top to be visible.
I'm using
let indexPath = IndexPath(row: index, section: 0)
tableViewActivity.scrollToRow(at: indexPath, at: .top, animated: true)
for this to scroll the selected row to top. This code is inside textFieldDidBeginEditing and is called correctly, but the tableView does nothing, there is no scroll. I found a post with a workaround for some by using a delay or reload the Data before scrolling, but nothing changed for me with this. So can anybody help me please?

Swift 2 - Update UITableViewRow in auto layout to change its height on button tapped in cell

I am using self sizing cell method to auto height in my tableview cell as my description text is variable(can fit in 1 to 5 rows). It is working fine.
Self Sizing Cell iOS 8
I also want that on button click inside the cell text of the description should be cleared and the row height should decrease. For this I have implemented following delegate to update description text to empty and than call a delegate to my tableview to update the row. but instead of resizeing and emptying the text in the description the text just goes back to description.
var delegate:UpdateRowDelegate?
#IBAction func btnTestTapped(sender: AnyObject) {
self.descriptionLabel.text = "-"//!self.addressLabel.hidden
if delegate != nil {
delegate!.updateRow(self)
}
}
Protocol
protocol UpdateRowDelegate{
func updateRow(cell:UITableViewCell)
}
Update row delegate implemented in table View controller
func updateRow(cell: UITableViewCell) {
let indexPath = tableView.indexPathForCell(cell)
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.endUpdates()
}
I am new to swift
When you do self.tableView.reloadRowsAtIndexPaths the cellForRowAtIndexPath will run again and it will probably set your description to the original text.

Clearing out UITableView

I need a way to clear out a UITableView. Btw, I can't just clear out the data source and reload the UITableView. I have to remove all the cells from the UITableView.
I can't just clear out the data source and reload the tableview
Well, that's too bad, because that's the answer. Clear out the data source, or at least throw a flag that causes numberOfSections to return 0 - and then reload the table view.
I have to remove all the cells from the tableview
Yup, well, that's exactly what will happen if you do what I just said.
Try the following code:
let cell = tableView.cellForRow(at: NSIndexPath(row: z, section: 0) as IndexPath) as! Cusotmcell
cell.contentView.removeFromSuperview()
Where z is index of cell