I have a collectionView inside a tableViewCell
for example:
credit: How to use StoryBoard quick build a collectionView inside UITableViewCell
I would like to reload the collectionView when I update information.
I have put a print in the collectionView's cellForItemAtIndexPath to test if it is being called but it isn't. How can I get the collectionView to reload?
I found out how! In my tableViewCell class I just need to link the collectionView as an outlet so in my tableViewCell's cellForRowAtIndexPath I just needed to call cell.collectionView.reloadData()
Simplest Way
for cell in tableView.visibleCells {
(cell as? YourTableViewCell)?.collectionView.reloadData()
}
Or
If you need to reload Specific UICollectionView
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? YourTableViewCell {
cell.collectionView.reloadData()
}
1st on tableview cell create this function:
func collectionReloadData(){
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
}
then call it from
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { ...
cell.collectionReloadData()
create tags for collection view with indexPath.row of UITableView and create an instance of the UICollectionView using the tag and reload !
Related
I have a custom uitableviewcell called customContactsTableViewCell. I am trying to use this code to update the uitableviewCell content
let cell1 = self.contactsTableView.dequeueReusableCell(withIdentifier: "contacts", for: IndexPath.init(row: index, section: 0)) as! customContactsTableViewCell
cell1.cellView.image = statusFinal.1
self.contactsTableView.reloadData()
But nothing happens. no errors and the cellview is not updated
Try:
let cell1 = tableView.cellForRow(at: IndexPath(row: index, section: 0)) as! customContactsTableViewCell
cell1.cellView.image = statusFinal.1
If you are created your tableview through Storyboard just go to your tableview and set cell custom class to customContactsTableViewCell And you'r cell identifier should be contacts if everything is already setup like this, then just check your these lines.
cell1.cellView.image = statusFinal.1
self.contactsTableView.reloadData()
After reloadData it redraw tableview again and setup data in it through this tableview Delegate function.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
/// your code
}
It will again update your data with old data, Just save this data statusFinal.1 in your data model and then whenever you require reload, it will get update through updated data. Hope so you got the point, if need more info let me know.
I need to update tableview cell without reloadRows and reloadData method in swift. Is It possible?
You can use tableview.indexPathsForVisibleRows if you wanna just update the ones on the screen and the others will be updated through cellForRow / willDisplayCell
Then you would traditionally do something like
tableview.indexPathsForVisibleRows.forEach {
if let cell = tableview.cellForRow(at: $0) as? MyCell {
cell.configure()
}
}
Try this piece of code:
UIView.setAnimationsEnabled(false)
self.yourTableView.beginUpdates()
self.yourTableView.reloadSections(NSIndexSet(index:indexToBeReload) as IndexSet, with: UITableViewRowAnimation.none)
self.yourTableView.endUpdates()
If You want to update cell content size, You should use beginUpdates() and endUpdates() to change content size of UITableViewCell, in this case heightForRowAtindexPath will called for each cell in tableView and update height of TableviewCell. for iOS > 10, You should prefer performBatchUpdates(_:completion:) instead of beginUpdates() and endUpdates().
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
For more information
https://appengineer.in/2018/07/11/resize-uitableviewcell-size-without-fluctuation-and-jerk/
https://developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates
Adding views programmatically into tableView cells making scroll jerky and slow. I am adding views programmatically into cell in the delegate function "CellForRowAt". I tried it through delegate function "WillDisplay" but the results are the same.
What is the best possible solution to achieve this?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"cell", for: indexPath) as! CustomCell
addViewsInCell(cell: cell, indexPath: indexPath)
return cell
}
func addViewsInCell(cell: CustomCell, indexPath: IndexPath) {
//here i am adding some views programmatically
for i in 0..<3 {
let customView: CustomView = Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)![0] as! customView
cell.customViewPlacement.addSubview(customView)
}
}
Cell views are reused. Adding a subview every time in cellForRowAt indexPath: you end up with multiple copies of the same subview. Instead you should check whether that subview is already added. You could mark your subview with a tag and then use the viewWithTag method to test its presence.
Don't add any subviews/custom views inside tableView: cellForRowAt indexPath:. This will cause those subviews to be added repeatedly whenever that cell loaded by the table view. Instead create a subclass of UITableViewCell and create your subviews inside that subclass if you are working programmatically. Or else if you are working with storyboards, you can create a dynamic prototype cell.
I have a UITableView Controller class, and inside of it is a String array.
The Table view has 1 cell prototype and in it a UICollectionView with 1 cell prototype.
The CollectionView is populated by passing the array into the tableview cell, which is a UICollectionView Delegate and DataSource.
When I make changes to the array inside the TableViewController, and call self.tableView.reloadData(), the CollectionView inside the cell does NOT update.
How do I fix this?
Thank you
EDIT:
Code in TableViewController:
#IBAction func addToArrayAction(sender: AnyObject) {
self.testArray.append("Ant-Man")
self.tableView.reloadData()
}
var testArray = ["Guardinas", "Iron Man", "Avengers", "Captain America"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("collectionContainerCell", forIndexPath: indexPath) as! TableViewCell
cell.testArray = self.testArray
return cell
}
Code in UITableViewCell:
var testArray: [String]?
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.movieTitleLabel.text = self.testArray![indexPath.item]
cell.movieYearLabel.text = "2016"
return cell
}
Please try to call reloadData of UICollectionView on every update in array.
If you are using only one cell in UITableView then you can reload in cellForRowAtIndexPath method of UITableView else you can can call after calling of reloadData of UITableView.
After I added cell.collectionView.reloadData(), CollectionView scrolling is so slow in my project. How to solved that kind of problem.
Having a UITableViewControllerand using:
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
return cell
}
Using dequeueReusableCellWithIdentifier:forIndexPath:'' instead ofdequeueReusableCellWithIdentifier`` how can I determine if a cell is a new one or a reused one?
Create your own custom cell class to use for the reuse identifier. Then, inside that cell, implement awakeFromNib and make your changes to the cell. This will only be called once when your cell is loaded from its NIB file.
Any other changes that need to be made when the cell is reused can be made in prepareForReuse.