iOS Swift ReloadData on UITableView does not refresh UICollectionView inside of UITableViewCell - ios

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.

Related

Get UITextView data in custom cell in swift

I have a custom cell with a UITextView in it and outlet for that UITextView is in the customCell file.
Now in my viewController I have tableView and I create and add that custom cell into my table.
After this I can't access UITextView and get its data as its outlet in the customCell file. I can't move the outlet to my viewController as I need it there.
How can I do this (access the UITextView in my viewController)?
You can set that delegate of cell's textView with your viewController, First set its delegate inside your cellForRowAtIndexPath
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableCell
cell.textView.delegate = self
cell.textView.tag = indexPath.row //In case you have multiple textView
return cell
}
Now you can get this textView inside its UITextViewDelegate method.
func textViewDidBeginEditing(textView: UITextView) {
print(textView.text)
}
You need to declare a local variable in your viewController to get the data of your UITextView. Then implement the tableViews delegate method
//this is your local variable declared in viewCOntroller
var myTextViewText: String?
//And this is your delegate method
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! YourCell
myTextViewText = cell.textView.text //here you will get the text of your cell's textView
}

how to reload a collectionview that is inside a tableviewcell

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 !

Get TableViewController from TableViewCell

I have a custom class for a table cell that is connected to a switch within the table cell (with an action) and I want to be able to to communicate to the TableViewController that the action happened as well as the path of the cell. The way that initially came to mind was if I could use some function in UITableViewCell to get the TableViewController of the table the cell is part of, as my custom class is (rather obviously) a subclass of UITableViewCell. Please tell me if I'm missing something.
To get a reference to the containing view controller, I add a weak property on the cell subclass.
#property (nonatomic, weak) UITableViewController *viewController;
You can assign this value in cellForRowAtIndexPath:
For accessing each cell in TableView
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourTableViewCell
cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]
return cell
}
For getting selected cell in TableView
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell;
print(currentCell)
}
Reaching from TableViewCell to TableView
self.superview //// self is TableViewCell and its superview represents TableViewController
hope that helps
You should make the ViewController the target of the switch action.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchCell", forIndexPath: indexPath) as! SwitchTableViewCell
cell.onSwitch.addTarget(self, action: Selector("cellSwitchDidChange:"), forControlEvents: .ValueChanged)
cell.label.text = "This is a Switch"
return cell
}
func cellSwitchDidChange(sender: UISwitch) {
let origin = sender.convertPoint(CGPointZero, toView: tableView)
let indexPath = tableView.indexPathForRowAtPoint(origin)!
// do something
}

IOS tableview issue

I'm having an issue where my app will freeze if when I press it to got the next scene which is a view controller with a tableview nested inside of it. the app will only freeze when I've declared how many rows the table will have. If anyone knows how to fix this problem its be greatly appreciated.
class ViewController: UIViewController, UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = "hello"
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Have you set dataSource of your table view? You must call tableView.dataSource = self somewhere in your code (most probably in viewDidLoad method)
The problem with your code is that in tableView(_:cellForRowAtIndexPath:) you are creating new cell every time the method is called. That is not how table views (or collection views) work.
The idea behind these components is to reuse their cells so that your application shows better performance. What if you have 1000 cells? You will create 1000 UITableViewCell objects for each cell? This is where dequeueReusableCellWithIdentifier(_:) comes in handy.
So set an identifier for your cell in Interface Builder, for example "Cell" and get the cell like this:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
This is the right way to go.
You have to make sure you set the correct reuse identifier for your table view cell (either in storyboard, or code).
Storyboard:
Of if you want to do it in code, put this in viewDidLoad():
tableView.registerClass(UITableViewCell, forHeaderFooterViewReuseIdentifier: "cellIdentifier")
Important to know that you should only use one of the above. If you define your UI in Storyboards, I recommend the first approach.
Then when you create the cell in tableView(_:, cellForRowAtIndexPath), instead of this:
var cell = UITableViewCell()
Use a cell from the reuse queue like so:
var cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! UITableViewCell
Everytime you are creating object for cell, make sure you use reuse identifier.
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

Subclassing UITableViewCell on Swift - Error edit UITableView rows

Using Swift: I've hooked up an NSFetchedResultsController to a UITableView, and created a subclass of UITableViewCells for the TableView cells.
The UITableViewCells format and display correctly. But the app crashes as soon as I try editing the rows (adding or deleting).
For testing, I've used the most basic subclass possible: An empty subclass, and it still crashes. The original UITableViewCell works normally.
Any ideas? Swift bug? The crash goes straight to the top of the stack. I get nothing in the console.
Works:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = task.desc
return cell
}
Doesn't work:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell2
let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = task.desc
return cell
}
When subclass is:
import UIKit
class UITableViewCell2: UITableViewCell {
}
If you create table cells programmatically (not in InterfaceBuilder) you have to register the table cell class with
TableView.registerClass(..., forCellReuseIdentifier...)

Resources